Why is Radix Sort so Fast? Part 2 Radix Sort

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ม.ค. 2025

ความคิดเห็น • 711

  • @BillTyler
    @BillTyler 3 ปีที่แล้ว +354

    Radix sort is exactly how punched card sorters worked in ancient times. The machine had a bin for each punch position. (There were 12 punch positions in each column.) The card deck was run through the sorter once for each digit position, and the bucket contents picked up in order, the deck restacked, and run through the sorter for the next punch position until the entire sort key had been covered. Sorting a couple of thousand cards could be done quite quickly.

    • @new_reZn
      @new_reZn 3 ปีที่แล้ว +6

      astonishing!

    • @vNCAwizard
      @vNCAwizard 3 ปีที่แล้ว +8

      Such equipment went by the name Unit Record Machines. This category of hardware included sorters, simple calculation tools (adders, multipliers, subtracts and dividers, for instance) and printers.

    • @deedewald1707
      @deedewald1707 3 ปีที่แล้ว +2

      Bucket sort at work being efficient !

    • @suntexi
      @suntexi 3 ปีที่แล้ว +5

      I worked in a clearing bank in London back in the '60s and we used the same technique to sort cheques by account number, by magnetising the digits on the cheques and reading them. Eight passes for the account number which also sorted in branch order. The cheques had already had anything other than those that belonged to the bank removed and sent to the other bank. London cheque clearing in action.

    • @joebarrett4353
      @joebarrett4353 3 ปีที่แล้ว +1

      Those were the days!!!

  • @ddc9999
    @ddc9999 4 ปีที่แล้ว +78

    I’m just a random 20yo student and I discovered this channel out of YT’s algorithm
    Loved it after only one vid, subbed and have been binge watching this on this rainy sunday afternoon, sipping coffee.
    My life is pointless but at least I’m learning cool stuff

    • @andrewwalker2888
      @andrewwalker2888 3 ปีที่แล้ว +11

      Hey mate, your life isn't pointless! I found this channel last night, and it's been great. I "graduated early" with a 2-year degree in electrical engineering a long while ago, never used it outside of school unfortunately.. and today I'm employed as a full stack dev with 7+ years of professional experience in that position. Life can be crazy, and I don't know much about it (yet!), but I know for sure, your life isn't pointless :) This is an old post, but hit me up if you wanna chat, or have any questions about anything. Much love.

    • @maxm9960
      @maxm9960 3 ปีที่แล้ว +2

      if you "really" learn this, by which I mean you understand to the degree that you can explain it to others well by yourself, you are already on track to a great career as a good software engineer. In time, you'll build yourself a good career at a software company like Microsoft or Google. Regardless of whether you like the companies, you'll at least set yourself up for a good salary which'll enable you to do what you like. Keep it up!! - source : I'm a senior software engineer at Google myself.

    • @markusballsdeep6051
      @markusballsdeep6051 3 ปีที่แล้ว

      Cringe

  • @bobwatkins1271
    @bobwatkins1271 4 ปีที่แล้ว +66

    Decrementing the prefix sum serves two purposes: 1) converting to a zero-based index, and 2) avoiding collisions in the output array. The latter is accomplished by updating the value stored in the prefix sum array. This is a crucial point that should be stated explicitly.

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +15

      You are right! Thank you for watching :)

    • @incription
      @incription ปีที่แล้ว +7

      I was confused at first, but the visualisation made it really easy to realise you can just increment the counter!

  • @awardofsky9908
    @awardofsky9908 4 ปีที่แล้ว +179

    I've seen many youtube videos and papers on this matter and I must say that I never have seen sorting algorithms explained so simply and with such passion!
    I really liked how you explained the logic behind comparison based sorts on part 1, it's really useful to understand sorting networks and their limitations.
    You are an excellent teacher and hands down one of the best youtube channels on this matter!
    Btw, just a note: to implement radix sort for signed integers you just need to do the prefix sum in the last digit that includes the signed bit in a different order, going from the middle of the array to end and then from the start to the middle again so that the digits starting with
    1 (the negatives) appear first.
    For floats the idea is similar, but instead, you also have to do the first partition of the prefix sum in reverse order (going from the end of the array to the middle, and then from the start to the middle again) and then you adjust the scattering part so it decrements the pointers corresponding to numbers from the first partition. There are also bit flipping solutions, so you don't need an extra radix pass.

    • @Wyld1one
      @Wyld1one ปีที่แล้ว

      Just have counts from -9 to 9. Eg 19 counts

    • @Redditard
      @Redditard ปีที่แล้ว

      @@Wyld1one it won't work, we start from the once's place, if you want to do -9 to 9 i'll add some overhead such as add +9 to your counts etc etc, and would also have to check the -ve every digit cycle

  • @timberwolf4242
    @timberwolf4242 4 ปีที่แล้ว +17

    This is literally the only channel on youtube about programming that i watch solely because the videos are made so engaging and fun, and not because i have to prepare for test.
    Best channel

  • @amdreallyfast
    @amdreallyfast 3 ปีที่แล้ว +92

    A few years ago I was working on a project to create a bounding volume hierarchy for collision detection, and as part of organizing the objects (along a space-filling curve for efficiency's sake) I followed some technical papers to create a GPU-based, parallel binary radix sort. It was fast, and the particles that were being collided never had to leave GPU memory. But it was a right beast.

    • @theRPGmaster
      @theRPGmaster 3 ปีที่แล้ว +12

      That's why I'm here, octrees!

    • @musicdev
      @musicdev 2 ปีที่แล้ว +8

      It takes a highly specific kind of nerd to call that awesome, but god damn, that is awesome!

  • @tucan1309
    @tucan1309 4 ปีที่แล้ว +409

    linked lists take a lot of memory
    me a python programmer:
    i have never heard of that guy

    • @jacknguyen5220
      @jacknguyen5220 4 ปีที่แล้ว +59

      me, a Java programmer {
      you missed the braces and semicolons;
      }

    • @cedric1731
      @cedric1731 4 ปีที่แล้ว +70

      @@jacknguyen5220 I doubt you are a Java Programmer... There is neither a class declaration nor any method indicator ;)

    • @qreeves
      @qreeves 4 ปีที่แล้ว +89

      Meanwhile, in Perl: !#$%&(%$%^#@#%&%^@#*

    • @binarycat1237
      @binarycat1237 4 ปีที่แล้ว +16

      Haskell: wait, there's an alternitive?

    • @bluesillybeard
      @bluesillybeard 4 ปีที่แล้ว +38

      me, an assembly programmer: what's a list?

  • @bluesillybeard
    @bluesillybeard 4 ปีที่แล้ว +47

    This year has been really nerdy for me. I learned Python, Java, and i'm watching this awesome series!

    • @TimeConvolution
      @TimeConvolution 3 ปีที่แล้ว +4

      That's awesome good for you

    • @АндрейОнищенко-з8х
      @АндрейОнищенко-з8х 3 ปีที่แล้ว +3

      I code in c++ and whole life is nerdy)

    • @davidlynch4202
      @davidlynch4202 3 ปีที่แล้ว +2

      Awesome, you should try c, c++ and your cpus assembly language to see what goes on under the hood

    • @bluesillybeard
      @bluesillybeard 3 ปีที่แล้ว

      @@davidlynch4202 I am learning C and C++, but x86 assembly is way out of my scope right now.

    • @1andrew123
      @1andrew123 3 ปีที่แล้ว

      @@bluesillybeard thats what i thought too before I started trying to learn it :P you could figure it out

  • @mikicerise6250
    @mikicerise6250 4 ปีที่แล้ว +15

    MIND. BLOWN. Never have I been so sad that my sorting list is short. xD

  • @KingThrillgore
    @KingThrillgore 4 ปีที่แล้ว +490

    Radix sort is truly a sort without comparison.
    Get it? Because Radix doesn't do comparisons

    • @shirosurfer8864
      @shirosurfer8864 4 ปีที่แล้ว +33

      Like discrete mathematics is the one that counts the most

    • @restcure
      @restcure 4 ปีที่แล้ว +30

      Glad *that's* sorted!

    • @NStripleseven
      @NStripleseven 3 ปีที่แล้ว +4

      I see what you did there

    • @leocomerford
      @leocomerford 3 ปีที่แล้ว +14

      Now, there's a time and a space for this sort of thing ...

  • @theMosen
    @theMosen 3 ปีที่แล้ว +1

    Ok, this just randomly turns up in my YT feed for no apparent reason, even though it's old and I haven't watched a coding video in literally years. But thanks, algorithm, for knowing me better than I know myself, because I loved it!

  • @TheMR-777
    @TheMR-777 4 ปีที่แล้ว +50

    Seriously Mate, your this series ... I am a CS Student of 3rd Semester. We haven't started Data Structures and Algorithms yet, but will soon be (from 15th of Sept). And I am feeling so lucky, that you are making such a Great Series of videos based on Algorithms! This Counting and Radix Sort got me curious to understand all types of Sorting and other Algorithms. All Thanks to you mate :)

  • @romanmueller3479
    @romanmueller3479 4 ปีที่แล้ว +2

    Was blown away by count sort. Simple yet genious idea. Thanks for this video.

  • @wlockuz4467
    @wlockuz4467 3 ปีที่แล้ว

    This is like the big interview you get after winning a championship, perfect!

  • @sbqp3
    @sbqp3 2 ปีที่แล้ว +1

    Awesome stuff.
    Around 15:30, when talking about how to apply the prefix sum, I was thinking of the interpretation that the prefix sum is the running total of numbers with lower digits. So in the first iteration for 721, which end with 1, you are looking up how many items end with 1 or lower. And for 779 you are looking up how many numbers end with 9 or lower, etc. For each number, that tells you how many elements that need to be placed before that number, which also tells you the index of where to place it.

  • @scorch855
    @scorch855 4 ปีที่แล้ว +1

    Your enthusiasm for sorting algorithms and computing in general is really engaging.

  • @Aristotle000001
    @Aristotle000001 4 ปีที่แล้ว +4

    I love the way you present yourself and talk about these subjects. I've learned great things on this channel that I didn't even ask to learn, but just watched for entertainment xD

  • @JJCUBER
    @JJCUBER 4 ปีที่แล้ว +83

    10:36 “0 to 9!” That’s quite the range for the array 😉

    • @Zeero3846
      @Zeero3846 4 ปีที่แล้ว +14

      That's a fact ...
      ... orial

    • @leyasep5919
      @leyasep5919 4 ปีที่แล้ว +3

      some amps even go to 11 !

  • @gaius_enceladus
    @gaius_enceladus 4 ปีที่แล้ว +51

    Your energy and accent remind me of Steve Irwin!
    "Crikey! It's a radix sort! Now, we'll just sneak up behind heeeem and grab heeeem behind the head....... "
    Cheers from New Zealand!

  • @SC1240
    @SC1240 4 ปีที่แล้ว +33

    Best channel on youtube hands down

  • @aiacfrosti1772
    @aiacfrosti1772 3 ปีที่แล้ว +6

    Radix sort is O(d * n), where d is the length of the elements. For sorting elements whose length is shorter than log(n) radix sort absolutely blazes through the list. If sorting a list whose elements lengths are longer than log(n) radix sort actually performs worse than its contemporaries. Given that a randomly generated list of unique packed keys has a longest length of log(n) (you eventually run out of unique elements at any given length and have to add another digit), the d * n starts looking suspiciously similar to nlogn.
    However if you have a very long list of short, non-unique keys (if you wanted to sort a list of dice rolls or something I guess) this is absolutely the best way to do it!

    • @GuillaumeGris
      @GuillaumeGris 3 ปีที่แล้ว

      If you have to sort a list of dice rolls, Just count them in an array of 6 bins. This will most likely be faster than radix. If for some reason you want a long list instead of a compact array with the same information, you can always generate it from the bin array in O(n) time

  • @fergalhennessy775
    @fergalhennessy775 หลายเดือนก่อน

    the prefix sum part is SO COOL thank you for this great video mr creel

  • @Lyam695
    @Lyam695 4 ปีที่แล้ว +1

    This has become one of my favourite comp sci channel. And this is not a small achievement, given the number of great resources on youtube.

  • @yorgle
    @yorgle 3 ปีที่แล้ว +3

    I love watching those animated sort algorithm demonstrations, and Radix sort was always one that looked neat... but your explanation is fantastic! I finally understand it, and goddamn, is it freaking clever! :D

  • @kzdm5255
    @kzdm5255 3 ปีที่แล้ว +24

    The complexity of the Radix Algorithm is dependent on the size of the largest number. So if you had just one max value 64bit integer, you would be running the counting algorithm 19 times.

    • @JohnNada-b9s
      @JohnNada-b9s ปีที่แล้ว

      Not really. If you do MSB radix sort it is perfectly possible to skip empty buckets and buckets of size one.
      As usual, real performance depends on implementation and data distribution.

    • @Slicerwizard
      @Slicerwizard ปีที่แล้ว

      Nothing stopping you from using 100, 1000 or 10,000 counters. Gets you down to ten, seven or five passes. 22 upvotes for you, but not a single bright bulb in the crowd for two years - yikes!

    • @jacey0479
      @jacey0479 ปีที่แล้ว

      ⁠​⁠​⁠
      No? Bc you will quickly run out of memory

    • @Slicerwizard
      @Slicerwizard ปีที่แล้ว

      @@jacey0479 You are mistaken. Why do you think memory will be exhausted?

    • @jacey0479
      @jacey0479 ปีที่แล้ว

      Your counters have to be < the size of the list. That is the problem with Radix sort, trying to find a balance between memory allocation and speed. Obv it also isn’t good for small lists.

  • @Antiorganizer
    @Antiorganizer ปีที่แล้ว

    You're a natural! Nice and concise and clear! Kudos for that!
    Tell me you're a high school, college, or university teacher!

  • @urouroniwa
    @urouroniwa 3 ปีที่แล้ว +1

    Right after I graduated from university, I did some travelling and ended up doing odd jobs at a travel tour company. It happened to be the end of the fiscal year and given that this was more decades ago than I'd like to admit, they had a mountain of physical files that needed sorting. Usually they spent 2 whole weeks to sort the files. They asked me to sort it. I can't remember exactly how I set it up, but I took over several desks and made my "buckets". Bucket sort works *really* well with physical files because concatenating them back into the original pile is simple :-). I got it all done in an afternoon and after that they thought I was a genius :-D

    • @michaelwilkes0
      @michaelwilkes0 ปีที่แล้ว

      seriously. when someone hands you several file cabinets of paper, the first step is to sit down and think.

  • @tatianabasileus
    @tatianabasileus 4 ปีที่แล้ว +118

    Seems like radix-sorting strings would work too, as long as they are made from a defined alphabet?

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +64

      Indeed it does! A viewer from the last video mentioned he did a talk on implementing Radix Sort for strings at JuliaCon last year! His talk can be viewed at: th-cam.com/video/C65vYWeN7-k/w-d-xo.html

    • @meneldal
      @meneldal 4 ปีที่แล้ว +52

      It's actually potentially much better with strings because comparisons can take a lot of time with strings, but accessing a single character is fast.
      The bad news is you probably can't make this work with unicode easily. A string comparison in unicode is already an unsolved problem so you may not care about that.

    • @vylbird8014
      @vylbird8014 4 ปีที่แล้ว +11

      @@meneldal You don't need your string comparison to be particularly meaningful to use it in a sort. You just need it to be consistent.

    • @meneldal
      @meneldal 4 ปีที่แล้ว +18

      @@vylbird8014 The problem in Unicode is two string can look identical when you render them but don't have the same representation. Even normalization doesn't deal with every subtle thing.
      So while it will be consistent, it may give you results you don't want.

    • @MatthewDempsky
      @MatthewDempsky 4 ปีที่แล้ว +8

      @@meneldal You just need to transform your Unicode strings into byte strings such that the desired ordering is preserved. For example, the C standard library includes strxfrm() and wcsxfrm() for exactly that purpose.

  • @Goejii
    @Goejii ปีที่แล้ว

    Great video!
    I'd never looked at how radix sort works under the hood. I paused the video at 16:36 and I started prototyping a radix sort algorithm... And it works! It is even faster than std::sort!
    Two points though:
    - is there a clever way to know when the array is sorted to avoid unnecessary iterations?
    - can this algorithm work with a user defined comparison function?

  • @sveinungchr
    @sveinungchr 4 ปีที่แล้ว

    Man.. The things youtube wants me to learn is amazing... Thank you for taking the time to make this topic clear, understandable and interesting.

  • @frale_2392
    @frale_2392 4 ปีที่แล้ว +2

    I discovered your channel like yesterday, and as a game programmer in training I love your videos, you always explain things perfectly. Keep up the good work

  • @rayredondo8160
    @rayredondo8160 4 ปีที่แล้ว +2

    Okay, this is awesome! I've been trying to figure out a smart way to implement Radix Sort for a while, and you just gave me a way! Thank you for the awesome videos, keep doing awesome!

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +1

      No worries mate! Links to the original sources as well as my own version of the code will be provided in the final video. I hope to upload it later today. Thank you for watching :)

  • @alialmans
    @alialmans 3 ปีที่แล้ว

    absolutely beautiful, human ingenuity at its finest

  • @jfk_the_second
    @jfk_the_second 3 ปีที่แล้ว +1

    Very informative and easy to understand! I think it was well-paced for beginners, as well as for people to whom the particular topic is novel, but the concepts familiar.

  • @rczeien
    @rczeien 3 ปีที่แล้ว

    It makes perfect sense as you explain it. It reminds me of when you sort by multiple fields, you do the most important last.

  • @AhmedKhaled-xp7dm
    @AhmedKhaled-xp7dm 10 หลายเดือนก่อน

    Beautiful content, Thanks for the amazing explanation and visuals, it's clear that you love what you are teaching and that makes the videos more than perfect

  • @Winnetou17
    @Winnetou17 4 ปีที่แล้ว +29

    10:41: "We've got the input list just here... same elements as before, I think..." "Well, you wrote it, mate!"
    =)) What's a Creel playing both the teacher and the student for our amusement.
    Question: for integers, if you know you're big-endian, isn't it easier to simply build the counter array to have power-of-two buckets (like 8 or 16) ? And then, instead of doing mod 10, simply read the relevant 3-4 bits, put in the relevant bucket, and so on. You also have to make sure to have the sign bit in it's own step. Also, would it help to construct all the count arrays at once, so you only traverse forward the array once ? It uses a bit more memory, but arrays of 16 integers... doesn't sound that bad. Worst case for normal integers would be to compare 64 bit signed integers, and that would amount to 64/4 + 1 = 17 count arrays (the extra one is for the sign count array).
    Cheers!

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +3

      Sry, just realized I replied to the wrong comment yesterday, haha! My response didn't make any sense. Anyway, thank you for watching :)

    • @Winnetou17
      @Winnetou17 4 ปีที่แล้ว

      @@WhatsACreel No, it made sense. Having ANDs instead of DIVs (well, mod by 10 I think would've been for taking the digits in base 10)

    • @deedewald1707
      @deedewald1707 3 ปีที่แล้ว +1

      @@WhatsACreel Performing AND on LS 4 bits, then AND on MS 4 bits with SHR to bucket count and sorting process used on byte size numbers ! If needed, Scale up by groups of 4 !

  • @NeilRoy
    @NeilRoy 4 ปีที่แล้ว +1

    I was puzzled at first, but the final part of the video cleared it up for me. Brilliant algorithm.

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว

      I know what you mean! It's such a clever trick! Cheers for watching brus :)

  • @lepatenteux592
    @lepatenteux592 3 ปีที่แล้ว +1

    I would like to see a pinned comment about repeating digits when decreasing the prefix index... I did not realize that you do not reset the prefix index and "accumulate" the -1 so that identical digits don't get the same spot... I had to go through many comments before I found the answer to that Interrogation...
    The rest of the video is really informative, thank you!

  • @AngrocSound
    @AngrocSound 3 ปีที่แล้ว +2

    Brilliant, and such an awesome explanation. Only improvement would be to mention to subtract 1 from the prefix sum for *each* time you move an input to the ouput, not only once (all the talk of moving from 0 to 1 base counting confused that part for me). Slow-mo'ing the part where you go through the rest of the items real fast helped me see what you where doing to make it work. Thanks for sharing!

    • @markt43
      @markt43 3 ปีที่แล้ว

      An alternative approach would be to shift all the counts to the right and set the count of 0 to zero. In this approach, you would iterate from left to right and increment the counts instead of decrementing them. You would not have to convert between base 0 and 1 in this approach.

  • @elliott8175
    @elliott8175 4 ปีที่แล้ว +3

    Great video series. Radix getting the attention it deserves!
    I would point out though that I don't think that Radix is a compromise between speed and memory (when compared to Counting Sort). If k is (1 + max_element - min_element), then counting sort has O(k) space complexity **and** O(n+k) time complexity (auxiliary array requires linear traversal: first to initialise to 0, then to load the solutions). This is just as much of a time problem as a memory problem, so Radix helps to handle both of these.

  • @Greedygoblingames
    @Greedygoblingames 4 ปีที่แล้ว +1

    An excellent explanation and demo of radix sort. Quality TH-cam content this is.

    • @bruceleenstra6181
      @bruceleenstra6181 4 ปีที่แล้ว

      I agree, very clearly explained. After watching this video, (plus the previous videos reference to a mask and right shift) I wrote a working 16 bucket radix sort in 10 minutes.

  • @retinizer7702
    @retinizer7702 4 ปีที่แล้ว +7

    I think I am in love with counting sort

  • @ke9tv
    @ke9tv ปีที่แล้ว

    Back in the day, I encountered a radix-2 sort that was coded for a PDP-8 with two LincTapes (or DECtapes, which were just LINCtapes rebranded). Random access tapes - there was a timing track so that you could read and write anywhere, not just at the end of the tape. The buckets were accumulated in odd- and even-numbered sectors on the output tape. There was a weird trick where alternating passes through the tapes read the tapes backward. Nothing like sorting a megabyte of data on a machine with 16 kilobytes of core memory!
    When I jot a job as a mainframer, I encountered five-tape read-backward polyphase merge. Insane!

  • @Alexander_Sannikov
    @Alexander_Sannikov 4 ปีที่แล้ว +6

    well strictly speaking, it's not O(n). it's O((n+b) *log k/log b), where k is the largest number you're sorting and b is the base of its integer representation. because of this, it might be better to use base-2 representation to minimize b term or to use base-10 or even something like base-256 representation to maximize the divisor.
    so assuming that k and b are constants is not always precise, because you might actually want to change them to make the algorithm run at maximum speed.

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว

      Right you are! The third video shows the code and performance of a base 256 Radix Sort! I agree that the performance is logarithmic when sorting data or arbitrary lengths. It's only O(n) when the length of the keys is fixed. Cheers for watching anywho, have a good one :)

    • @acasualviewer5861
      @acasualviewer5861 2 ปีที่แล้ว

      I would call it O(N log R) where R is the number of bits. No need to change base as that is just a constant that gets simplified away by the definition of Big-O
      But I agree that it's not fully honest to call it O(N).

  • @yashpandey9550
    @yashpandey9550 4 ปีที่แล้ว

    One of the best sorting videos I have seen so far. Great work!

  • @hacked2123
    @hacked2123 4 ปีที่แล้ว +4

    Watched the first video and was going to propose what you called "counting sort"; my idea was to determine min/max and only use counts that fall in that range.

  • @chrisburke1672
    @chrisburke1672 4 ปีที่แล้ว +1

    Just watched the first one, nice job posting the second part so quickly

  • @serenasamborski6856
    @serenasamborski6856 4 ปีที่แล้ว +1

    You did such a good job explaining this! I’m just a self taught programmer but i had no problem understanding this! And it’s really interesting too!

  • @islandfireballkill
    @islandfireballkill 4 ปีที่แล้ว +47

    Nice video, but I have to complain about the supposition that radix sort is somehow O(n). Its extremely important to leave in the O(nd) where d is the number of digits.
    O(n) invites an extremely dangerous line of thinking where somehow people may come to the conclusion that radix sort somehow asymptotically beats nlog(n). It does in an extremely limited sense but its hardly the full story.
    Unless you have an exponentially increasing fraction of duplicates, then d must be of the same order as log(n). You can't represent 10,000 distinct numbers without at least 5 digits or ~log2(10)*5 = 15 bits. Suppose you are infact beating those bounds and d < log(n). Then at most (base^d/n) fraction of your array can be unique values by the pigeonhole principle. Concretely if you are beating it by 2 decimal digits or like 6.6 bits then at most 1% of your array is unique an the other 99% must be duplicates.
    Plus it makes it seem like log(n) is much faster growing then it is. In practice log(n) is strictly less then 64 on most machines. Its really not fair to make a "fixed word length" argument for radix sort and other comparision sorts because in reality you could never sort more then 2^64 elements on a machine with 64 bits of address space anyway.

    • @elliott8175
      @elliott8175 4 ปีที่แล้ว +20

      Hello, I've got to agree and disagree. I hope that Mr Creel will talk more about the complexity in later videos, but I'd like to point out that the time complexity of Radix is actually *O(log[base b](k) * [n + b])* , or if you prefer *O(log(k)/log(b)*[n + b])* , where *b* is the number of _bins_ in the counting array (10, in our case), and *k = 1 + max_element - min_element* . Note that *b=10* was used in the video for _simplicity_ but its value should be dependent on the array input.
      If we set *b=n* , then *O(log[base b](k)*[n + b])* becomes *O(n*log[base n](k))* . We usually abbreviate *log[base n](k)* to *d* , but as you can see, how we count the number of digits changes as the input size grows.
      Informally: As the input size grows, Radix becomes increasingly insensitive to *k* even if *k* grows polynomially with respect to the size of the array.
      Radix is a bad choice when *k* is high _and_ *n* is small; specifically when *k > n^log(n)*

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +32

      I’d say you are both right! The proper time complexity is O(nd). Keys are often fixed in size. Not always, of course, but often. We tend to use 256 as the radix, resulting in 4 iterations with 32 bit unsigned ints. So, it’s true that radix sort’s time complexity is more subtle than O(n), but it’s also true that in practice it tends to perform at about O(n).
      I’d add that the time complexity for many things is subtle. Addition, for instance, requires O(1), so long as you have a machine that has an Add instruction with an arbitrary number of bits. Otherwise, it’s O(n) with n being the number of bits. Measuring the performance of algorithms is deep and complicated.
      Thank you for the info, and thank you for watching :)

    • @SemiMono
      @SemiMono 4 ปีที่แล้ว +13

      And this is why I generally use big O notation for making quick estimates, but not as a final decision maker. It's a very coarse measurement.

    • @Alexander_Sannikov
      @Alexander_Sannikov 4 ปีที่แล้ว +3

      @@elliott8175 that is correct, i found your comment right after explaining the exact same thing in mine. In O((n+b) log k / log b) each value can become a bottleneck. Sure, most of the time N will bottleneck, but so can higher than optimal base and higher than optional range. Especially when sorting binaries, fixed point floats, strings, etc.

    • @guymella
      @guymella 3 ปีที่แล้ว

      The way he explains radix sort... yes his version is not linear. In real radix sorts you always go most significant bit first. This means that after the first iteration you have a mostly sorted array and boundaries for all unsorted portions so you can recurse and sort the sub arrays or switch to quick sort in the subarrays. Aka burstsort.

  • @eugenematison5571
    @eugenematison5571 ปีที่แล้ว

    Great explanation! Thank you! One small remark. Unfortunately, by adding some offset to float and then substracting the same value you will not end with original float value. Even more, adding offset to some float values can make them indistinguishable.

  • @JesusP7
    @JesusP7 4 ปีที่แล้ว

    Don't know why youtube recommended me this, but for once the algorithm got it right! Didn't know i wanted to know this.
    Perfectly explained, great step-by-step examples. Thank you for the content

  • @JamesSmith-fg8hp
    @JamesSmith-fg8hp 4 ปีที่แล้ว +3

    Thank you for the video, I really enjoyed watching it.
    I would want, however, to put an emphasis on the witchcraft that let you rebuild sorted array by a prefix sum, as this is the part that confused me for a bit, but I actually have finally realized it and I want to help others as well.
    So, to the explanation. There are two major points why the radix sort works. The first is, on each iteration it sorts values by one digit. How does prefix sum helps it? The idea is that the prefix sum (at the index 7, exempli gratia) is the amount of numbers whose last digit is less or equal to 7 (say this amount is 4). And the prefix sum at the index 8 shows the amount of numbers whose last digit is less than equal to 8 (let it be 6). So in a sorted by last digit array the indices 0,1,2,3 would be occupied by numbers with last digits less or equal to 7, which clearly means that indices 4, 5 would contain numbers with their last digit equal to 8. I really liked this trick, thank you for showing it to me.
    The second part of radix sort is it's stability, but it's trivial compared to the witchcraft above.

    • @apuji7555
      @apuji7555 3 ปีที่แล้ว

      Wow, this makes a lot of sense. Thanks!

  • @firefly618
    @firefly618 3 ปีที่แล้ว

    So the overall lesson is: know your data.
    The digits of a base-10 number, as well as the bytes or nibbles or even bits of a base-2 number, contain much more information than just x < y. Because every digit (or byte, bit...) is already classifying the number according to some power of 10 (or 2). If you can exploit that, you don't need to perform N log N comparisons. In fact, I think the same technique can be applied to fixed- or bounded-width strings, dates and times, tuples...
    By the way, I didn't know N log N came from log N!, that's very interesting. Here's a simple proof I found online (not taking the credit)
    Proof that O(log n!) ⊆ O(n log n):
    *log n!* = log(1 · 2 ⋯ n) = log 1 + log 2 + ⋯ + log n ** log n/2 + ⋯ + log n *>* ⌊n/2⌋ · log ⌊n/2⌋ ∊ O(n log n)
    Therefore O(log n!) = O(n log n) ∎

  • @eugenematison5571
    @eugenematison5571 ปีที่แล้ว

    Did not see other videos from this channel yet. But this one is enough to be subscribed! ))

  • @hendrixgryspeerdt2085
    @hendrixgryspeerdt2085 3 ปีที่แล้ว

    When building the prefix sum , if you begin one index offset, (sum in index 0 is 0, second index 1 is sum of 0’s and so on). Then if you iterate from left to right when building the output array, you would place the element in the index specified by the prefix sum and then increment count.

  • @vukpsodorov5446
    @vukpsodorov5446 4 ปีที่แล้ว +1

    awesome stuff! also you're really good at explaining all this, it's all so simple to understand compared to some other explanations out there, awesome work!

  • @meanmole3212
    @meanmole3212 4 ปีที่แล้ว +4

    Hearing "Good stuff!" takes me back to 2014-2015 :D

  • @linobigatti
    @linobigatti 4 ปีที่แล้ว +2

    Radix sort is plain magic, and youve explained it perfectly ö

  • @neverremindyou6172
    @neverremindyou6172 4 ปีที่แล้ว +56

    Right now I´m studying "Computer Science" and i am learning for something we call "Algorithms and Datastructures". These 3 videos were actually shorter and way better than the ones from university. I can´t find something for big O notation in general from you. Do you have Videos for that topic? Anyway i reallly love your vids! Keep on Doing these! And sorry for bad english.

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +26

      Your English is great! I don't have any vids on Big O, but that's a great idea! Thank for watching, and thank you for the suggestion :)

    • @TheMR-777
      @TheMR-777 4 ปีที่แล้ว +3

      Wait ... For Big 'O Notation, you can check the channel of : *CS Dojo* . When "What's Creel" said about Big 'O notations, I instantly jumped to his tutorial :), and got the concept. Then came here to understand this. By the way, Great explanation.

    • @sjainsjain-hj4oq
      @sjainsjain-hj4oq 4 ปีที่แล้ว

      @@WhatsACreel caaan you make big 0 video sir covering topics from CLRS bible..I am not able to understand that book.

    • @T3BFitnessTripleThreatBoss
      @T3BFitnessTripleThreatBoss 4 ปีที่แล้ว +2

      Download EDX app on your phone and do the CS50 Introduction to Computer Science from Havard.

    • @milkYw4iMC
      @milkYw4iMC 4 ปีที่แล้ว +1

      Aachen? x)

  • @kensmith5694
    @kensmith5694 4 ปีที่แล้ว +2

    The "bucket" version of radix sort is exactly how you sorted cards in an old mechanical punch card sorter. It took as many passes as there are digits. So you had to pull the cards out of the output stacks and stack them up in the input.
    Programs stored on punch cards would often be numbered in steps of ten in the last 8 columns. This way to insert a line after line #1000 of a 100 card stack, you would number the new card as 1001.
    If you go with 16 counters (AKA 4 bit digits), the counters can be on the stack. On most machines, allocating 16 words of stack takes no extra time at the routine entry.
    Sorting strings of text or other large things can often be more efficient if you make arrays of pointers.

    • @rhymereason3449
      @rhymereason3449 4 ปีที่แล้ว

      That's right! I forgot about that... anyone who ever dropped their program card deck (or who's wise cracking buddy gave it a toss) but was wise enough to put line numbers on the cards knows all about this.

    • @kensmith5694
      @kensmith5694 4 ปีที่แล้ว

      @@rhymereason3449
      The new discussion of it is sort of an "everything old is new again" thing. The cards were invented for the US census before computers were machines. The mechanical "tabulator" let the government work out how many plumbers owned cats without any electronics needed.

  • @dodogo777
    @dodogo777 3 ปีที่แล้ว

    superb explaination! by far the most intuitive explanation I watched on radix sort. Thank you!

  • @strangeWaters
    @strangeWaters 2 ปีที่แล้ว +1

    You can iterate the input array from the left if you use an exclusive prefix scan instead (add up all the elements up before but not including the slot)

  • @willzin-da-esfiha
    @willzin-da-esfiha ปีที่แล้ว

    I was implementing radix sort by my own and I was surprised when my algorithm managed a total of 10GB RAM (not at same time, of course, but the sum of all allocating and deallocating operations).
    To improve it first I tried to do a better approach for bucket vector: instead of allocating 1 extra position by push operation, I would double the allocated size and use a second length prop to measure the vector. After that, I could reduce the 10GB total handled RAM to 400MB.
    Watching the video, an idea instantly clicked in my mind when I saw the digit counting array: "I CAN PREDICT THE BUCKET SIZE BY ITERATION!!!!" then I stopped video and did it. Reduced from 400MB to 100MB total handled memory.

  • @xavierdupont5772
    @xavierdupont5772 3 ปีที่แล้ว

    Thank you man.
    I discovered radix sort in this crazy guy videos that make animated sorting algorithms with sound, and it's a lot of fun to watch, but not I finally understand how it works. It's a kind of magic ;).

  • @stevent887
    @stevent887 4 ปีที่แล้ว +1

    So maybe I'm missing something simple here, but when you calculate the Prefix Sums, I understand *how* several of them ended up as 5, because there are multiple entries which all have 6 as the second digit. But, how do you account for that in the code? The way its explained, the 5-1 would mean index 4 into a 0-based array. Since multiples land in the same slot of the array, do you have to check that area to see if it contains a value? This is the part of the video around 18:55 where it just says "We repeat the same for all input elements..." But pass 1 and pass 3 don't have any duplicate indices. How do you account for those?

    • @Joel-co3xl
      @Joel-co3xl 4 ปีที่แล้ว +1

      I think you can subtract from the prefix sum each time to get the next index

    • @MattMcConaha
      @MattMcConaha 4 ปีที่แล้ว

      @@Joel-co3xl Yep, I'd say for the sake of explanation that you calculate a prefix sum and then stop calling it a prefix sum. Once you start doing to actual assignment operations you start using that prefix sum array as a mutable pointer array.
      The prefix sum tells you at the beginning where the first number in each bucket number should be placed. Before you start placing anything, stop calling it a prefix sum, it's not a prefix sum anymore now it is a bucket pointer. So when you go down your array, each number belongs to a certain bucket, that bucket has a pointer which says where the number should be placed in the new array, and you decrement that pointer so that the next number that comes along in the bucket will have the correct pointer. After you do any decrements, I'd argue that the array is not actually a prefix sum anymore, which is why I personally would urge you to not call it one anymore.
      Another way you could hypothetically do it is to keep your prefix sum array and also store another array which keeps track of how many numbers have been placed in each bucket so far. So you would know that the prefix sum value minus this new counter value is where you need to place your given element. Granted this is an objectively worse system, it does literally the exact same thing but using extra ram and less efficient subtraction calculations.

  • @pepe6666
    @pepe6666 ปีที่แล้ว

    thanks mate i appreciate your approach to these videos. you are very human and i can really get a feel for the worth / importance of each thing you mention. thanks man

  • @trolledwoods377
    @trolledwoods377 4 ปีที่แล้ว +1

    If you subtract one from the first element when building the prefix sum(making sure to use a wrapping subtract to avoid overflow(unless you're using signed numbers), it should remove the awkward -1 for each step.

    • @nispelsm
      @nispelsm 3 ปีที่แล้ว +1

      It wasn't explained very well in the video, but you are actually subtracting 1 from that digit's prefix *each* time you copy a value from the source array to the destination array. Let's say your original array has three "5"s, and the starting prefix for that digit is 9. The first time you add the "5" to the destination array, you subtract 1 from the prefix, placing it at index 8, but the 2nd time you add the "5" to the destination array, you subtract 1 AGAIN from the prefix, making it index 7, and so on.

  • @timmurphy5541
    @timmurphy5541 3 ปีที่แล้ว

    This was very good. I wish that more had been put into explaining the prefix sum - some of the mechanics were quite obvious and didn't need as much explanation as that did. I know it's a lot of work to do these things so thank you and at least it has given me a question to find an answer for.

    • @timmurphy5541
      @timmurphy5541 3 ปีที่แล้ว

      ah, Ding! now I get it - after writing that...duh. :-)

  • @ddichny
    @ddichny ปีที่แล้ว

    Some clarification:
    6:35 "Hey presto, we haven't got anything sorted at all" (after the first pass). This glosses over the important fact that at the end of the first pass, the last digits *are* sorted. Indeed, each pass sorts the whole list via the digit being scanned in that pass.
    Another key feature that should be mentioned is that the method of stacking the buckets preserves the order the numbers were already in, and thus preserves the lower-digit ordering(s) imposed by the earlier passes even as you're sorting the new current digit. And so does the method of unstacking the buckets back to the output array. Without this, each new pass could scramble and lose the low-digit ordering imposed by the previous passes. But with it, the final (highest-digit) pass is guaranteed to leave everything in a fully sorted condition, since it preserves the already-ordered runs of the lower digits.
    This property (of preserving the prior ordering of same/bucketed items during a new sort) is called "sort stability", and is very useful when constructing multipass algorithms. Meanwhile, most efficient sort algorithms are "unstable" and don't guarantee preserving any prior ordering. For example, if you quicksort a list of employees by name, and then quicksort that by sex, the resulting list will have all the males together, but their names will likely end up scrambled again. But if you use a stable sort algorithm and do the name sort followed by sex sort two-pass process, the guys on the final list will still have their names alphabetized.

    • @ddichny
      @ddichny ปีที่แล้ว

      Incidentally, you can do a "stable" sort even with an "unstable" sorting algorithm by including the item index number in the comparison key. In the above example, don't just sort employees by "sex" on the second pass, generate temporary "line numbers" for the alphabetically sorted list (1 for first item, 2 for second item, etc.) and then sort by "sex"+"line number".
      (In this simplified example, you could of course just do a one-pass sort on the key "sex"+"name" to get the job done, but most cases of multipass sorting aren't so easily collapsed, and that's where stable sorting and the line-number trick become quite useful.)

  • @brucea9871
    @brucea9871 3 ปีที่แล้ว

    This is the first time I've heard of counting and radix sorts. I've always assumed that quicksort and merge sort were the fastest sorts. But once you began describing counting sorts I quickly got the idea of how it worked.

    • @MMasterDE
      @MMasterDE 3 ปีที่แล้ว

      Hehe, there's a lot of sorting algorithms that are way faster or have better worst cases than the usually default implementations, but they usually consider the data to be sorted and may require more memory. Quicksort doesn't require any extra memory. Another example of better worst case ("faster big O notation") that works with pretty much any comparable data, that has better worst case, only takes up about twice the memory, easily implemented and understood, is tree sort. Just insert items into a self balancing tree (log(n)), then traverse the tree in order AKA O(n*log(n)). That's a very stable sort, best, average and worst case is all the same.

  • @robertlittlejohn8666
    @robertlittlejohn8666 3 ปีที่แล้ว +3

    I;m old enough to remember how the old IBM punched card machines were used for sorting. It was a radix sort, and the "buckets" were actual buckets or slots that a card would fall into, if, say, column 35 were punched with a 3. You'd then gather the 10 buckets up in order, and run the deck through again, soring on column 36 (or whatever).

  • @Fanaro
    @Fanaro ปีที่แล้ว

    What an underrated channel!

  • @NbWfer
    @NbWfer 3 ปีที่แล้ว +1

    For computers, might be more sensible to use binary counting system for the bucketing so, you can use the speed of faster bit-shift and bitwize operations for the bucketing -if used with signed/unsigned numbers, and not with 2's complement numbering system

  • @RickeyBowers
    @RickeyBowers 4 ปีที่แล้ว +2

    Well done, loved the graphic work you put into your videos. Of course, some of the radix sorting can be done in parallel.

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว

      Indeed it can, yes! Cheers for watching mate, have a good one :)

  • @xipheonj
    @xipheonj 3 ปีที่แล้ว

    Thank you youtube algorithm, it has decided that I needed to watch this today, and it was right.

  • @davidjames1684
    @davidjames1684 3 ปีที่แล้ว

    Also one thing worth noting is for input numbers with lots of digits, it takes some time and processor power to extract each digit. Also for very long numbers like 32 bit, which can be as high as 4,294,967,295 using standard binary encoding, that is a lot of passes you need to make, 10 total on each.

    • @simontillson482
      @simontillson482 ปีที่แล้ว +1

      I think for 32 bit numbers, you’d usually only consider 8 bits at a time so 256 counters and four passes would do it. Nobody would consider sorting 32 bits numbers in decimal. It just makes no sense when bytes are easily extracted.

  • @codewizard58
    @codewizard58 3 ปีที่แล้ว +50

    I didn't see you mention that when building the output array, you increment the index in the count array, to handle where the same digit is repeated. The prefix sum has already taken this into account.

    • @SergeyNeskhodovskiy
      @SergeyNeskhodovskiy 3 ปีที่แล้ว +17

      Also had to replay a couple of times to understand this, but I believe it's DEcrement rather then INcrement, right? He decrements the prefix sum and the next time the same digit occurs the prefix sum is already decremented and gets decremented once again

    • @peterhobo
      @peterhobo 3 ปีที่แล้ว +5

      @@SergeyNeskhodovskiy yes, and that's also why you move from the end of the array to the start. You move the higher numbers into position first, then decrement so that the next position in the prefix sum is next highest number

    • @disekjoumoer
      @disekjoumoer 3 ปีที่แล้ว +9

      This is actually pretty irritating. He decrements the index of each value once it is accessed so that repeat numbers don't land up in the same spot. It's a major omission that will cause people who don't know what a radix sort is.

    • @peterhobo
      @peterhobo 3 ปีที่แล้ว +10

      @@disekjoumoer he certain does mention that you decrement the values in the prefix sum. Do you mean he didn't take the additional step of explaining the second purpose of it?

    • @kcaz64
      @kcaz64 3 ปีที่แล้ว +1

      @@disekjoumoer He does mention that though. I agree he should have made it a bit more explicit, but he *does* say you need to decrement it after accessing it.

  • @Wyld1one
    @Wyld1one ปีที่แล้ว

    Can be used on variable length strings too. numbers are just strings with just two counts (0 & 1)
    Short cut too: if you run out of digits have a extra count to indicate 'already sorted' and treat like -1.then you can skip all the items in the next round (bin) that was -1 (character strings go left to right,numbers right to left)

  • @amkhrjee
    @amkhrjee ปีที่แล้ว

    Amazing mini-series! Thanks a ton for taking the time and effort to make the beautiful presentations. Much appreciated. And, on a different note, is that a cat holding a cricket bat? I'm talking about the picture behind you.

  • @MadhavaVishnubhatla
    @MadhavaVishnubhatla 4 ปีที่แล้ว +67

    I love you my guy

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +10

      Ha! You too

    • @kippenegg
      @kippenegg 4 ปีที่แล้ว +1

      Same, this series is so interesting

  • @vvill-ga
    @vvill-ga 4 ปีที่แล้ว +6

    Please do a video on how Radix Sort performs with differently based numbering systems, like binary or hex. I'd assume larger based numbers rely more on storage and read/write speeds, and smaller based numbers like binary rely on processing speed instead. Thank you!
    EDIT: Sorry, didn't see the other video you posted. Watching it now!

  • @KetilDuna
    @KetilDuna ปีที่แล้ว

    That was a really neat explanation of a smart sorting method. Thank you for sharing.

  • @winstonsmith8597
    @winstonsmith8597 3 ปีที่แล้ว

    I have a large array been waiting for this sort

  • @SosetaFurioasaJr
    @SosetaFurioasaJr 3 ปีที่แล้ว

    G'day, matey! Thank you again, here's one for the algorithm ! Btw, loved the little joke between you and the presentation with the same different set of numbers, haha.

  • @Jankoekepannekoek
    @Jankoekepannekoek 4 ปีที่แล้ว +7

    I feel like the principle of analyzing the digits of a number can be generalized to other types as long as those types are deconstructable in a similar way. Might be nice to implement using the type-class pattern.

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +1

      That's a great idea! Anything that generalizes to ints and you're good to go! Cheers for watching mate :)

    • @hdauven8434
      @hdauven8434 4 ปีที่แล้ว

      In some languages counting sort can easily be used for character sorting (and thus be part of a larger algorithm) because a lot of languages map chars to numbers.

    • @peepeefrog_
      @peepeefrog_ 4 ปีที่แล้ว +1

      @@hdauven8434 That is ASCII or UTF, not languages

    • @hdauven8434
      @hdauven8434 4 ปีที่แล้ว

      @@peepeefrog_ Notice the "easily", as in you do not need to write any conversion code. If you want to apply this algorithm to Java you would need to parse a char to an int. In JavaScript you can just apply a comparison to an individual character directly.
      My point was that in some languages the algorithm doesn't need an extra step because there is no special datatype abstraction between a character representation and ASCII/UTF.

  • @jursamaj
    @jursamaj 3 ปีที่แล้ว +1

    I feel like there's a better way to generate the prefix sums:
    A. Set the prefix for 0 to 0.
    B. Iterating n from 0 to N-1, set prefix[n+1]=prefix[n]+count[n].
    Doing this yields 0-based indices to the location to store the first item of each n, instead of 1-based indices to where to store the last. Now you can iterate over the original data left to right again, storing each in the slot for its prefix, then increment that prefix.
    I know, both methods yield exactly the same result. This way just feels better to me.

    • @panjak323
      @panjak323 ปีที่แล้ว

      How do you solve collisions ? You have subtract 1 anyways on each repeating element.

    • @jursamaj
      @jursamaj ปีที่แล้ว

      @@panjak323 There are no collisions. And you don't have to decrement, you have to increment. Notice I said go left to right, not his right to left.

    • @panjak323
      @panjak323 ปีที่แล้ว

      @@jursamaj what are you talking about? Look at the next video, he is clearly decrementing counters for each number contained in the bucket... Otherwise how else would you know where to put 2 elements ending with same number in the output array? Without decrementing the counter each time you would put them in the same spot. Your method is totally redundant since only difference it would make would be counter[i]-- vs --counter[i].

    • @jursamaj
      @jursamaj ปีที่แล้ว

      @@panjak323 Sorry, but you didn't understand what I said above. Creel has to fill the array backwards & pre-decrement the indices because his calculated indices point past the end of each bucket. My method points to the beginning of each bucket, so you fill the array forward and post-increment the indices.

  • @hagazissa1599
    @hagazissa1599 3 ปีที่แล้ว

    I don't quite get why the prefix sum even is needed at all, in my head another possibility would be the following:
    1. Count the number of least significant digits (like done in the video) of each array element.
    2. Repeat for each of the digits.
    The result would be n count-lists, each taking up space of at most 10 integers (if the input array is in base10), with each element being at most array_length large, which could be displayed in log(array_length).
    This can probably be optimized by pre-initializing all the count list, then reading all the input array digits, that way you would only have to go through the input array once.
    Now recreate the final sorted list with the following algorithm:
    1. Check all created lists, and set the least significant digit of all the output-array elements to the corresponding number in the count list. For example, if the count list returned "5" for the 0 entry, the first 5 entries of our output-array would have the least significant digit 0. If the count list returned "0" for the 1 entry, the next 0 entries would have the digit 1. If the count list returned 9 for the 2 entry, the next 9 entries of our output-array would have the least significant digit 2.
    2. Repeat with the next least-significant digit (e.g. the ten digit).
    3. After n runs the list should be finished and sorted.
    This can also probably be optimized, to "build" each output-array-element by combining the information of all the count-lists at the same time. Like: "The first element must have a 3 1-digit, a 2 10-digit, and a 6 100-digit, so the first element will be 623". Not sure about the runtime of this particular example though.
    This should safe the writing to the output array, since you only write once, and since you don't calculate the prefix sums, you save on some additions. I think, the memory trade off might be on the harsher end, since for each digit of the element, you would need one list to store the counts in. For the array in the video that would be 3 lists, each with a maximum of value of 16 (since at most one entry of the count list can have 16, meaning all digits in the unsorted list would be the same). On the other hand, you wouldn't be needing any complicating list, so no double linked list or anything like that.
    If anyone could go over my example and tell me what is wrong with it, I'd be glad to hear!

  • @mikolajwojnicki2169
    @mikolajwojnicki2169 4 ปีที่แล้ว +1

    Excellent video!
    Am I wrong to think that you don't need pointers to swap the arrays? You just use the output array as your input array.

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว +1

      Yes, you can indeed! That's pretty much what the pointer switching trick does. Pointer switching is the code equivalent of using the output as the input and vice versa. Cheers for watching mate :)

  • @wonay
    @wonay 4 ปีที่แล้ว +1

    is there a reason why you can't do the radix counting sort starting by the third left digit and going to the 2nd and to the unit ?
    That would make the building of the output array much easier since you could just write for instance 300 , 300, 400, 500, 500, 500, ...
    and then add the second digit in the mix 310, 320, 420, 540, 550, 590. and then add the last digit ?
    That would eliminate the flipping of input/output array as well as the sum in the counter as well as the re read backward of the input array as well as the reindexing.
    That would add a lot of comparaison to find the biggest digit, right ?
    To know how far left digit to start with ?

    • @WhatsACreel
      @WhatsACreel  4 ปีที่แล้ว

      In this video we looked at LSD, least significant digit radix sort. There is certainly an MSD, most significant digit version too! Cheers for watching mate :)

    • @wonay
      @wonay 4 ปีที่แล้ว

      @@WhatsACreel ah thank you ! Is it more efficient ?

  • @epictetzu85
    @epictetzu85 3 ปีที่แล้ว

    8.5k likes with zero dislikes is very impressive. Well done!

  • @customraspi
    @customraspi 4 ปีที่แล้ว

    Radix Sort is basically a quick sort with a base greater than 2 (quick sort is a radix sort with only 2 different radices: those elements below the pivot and those above)

  • @bakedbeings
    @bakedbeings 4 ปีที่แล้ว

    This is a man who loves it.

  • @MisterFanwank
    @MisterFanwank 3 ปีที่แล้ว +1

    If you're using a custom allocator then the time to allocate may be trivial.

  • @CrystlizeWorld
    @CrystlizeWorld 4 ปีที่แล้ว

    Mate unreal content, love you from your beginners to C++ but this stuff is top!

  • @burakhanaksoy
    @burakhanaksoy 3 ปีที่แล้ว

    at 0:52, isn't worst case for 32 elements N * Log2(N) = 160?

  • @georgewashington90
    @georgewashington90 3 ปีที่แล้ว +1

    If you change memory addressing before starting input data into computer you do not need any algorithm sort to get sorted output. That is the fastest way of getting sorted data without any sorting algorithm. It is possible because memory addresses are already sorted. I made it in C and that is why I know it works, it simplifies many other things too (comparison numbers ,strings). For instance you do not need mathematical formula to state which number or string is greater, it is determined by which string or number is read from memory first. There is also very nice information overlapping in memory which does not lose information and makes memory much more efficient without compression.

    • @simontillson482
      @simontillson482 ปีที่แล้ว

      I think you”re describing an insertion sort there.

  • @atilafernandes217
    @atilafernandes217 4 ปีที่แล้ว

    [21:22] And what about if you already have the arrays, previously, for a small size sort?

  • @jaimeduncan6167
    @jaimeduncan6167 3 ปีที่แล้ว

    For people starting: The first algorithm as expressed does comparison, but not between the elements but between the numbers and the values on the array. The kind is “is it a 1” the reason he says correctly no comparison is because he has the implementation in his head. Like this m[n]+=1 where m is the count vector and n is the element in the input array. So yes it’s lineal. Notice that this is the case vaciar we are exploiting the nature of the data.

  • @HungPham2411
    @HungPham2411 4 ปีที่แล้ว

    The visualization in the video is very clear and easy to understand. Programmers may good at algorithms but not many of them good at describing.