2

I'm going through all the known / typical sorting algorithms (insertion, bubble, selection, quick, merge sort..) and now I just read about radix sort.

I think I have understood its concept but I still wonder how it could be done in-place? Let me explain how I understood it:

It's made up of 2 phases: Partitioning and Collecting. They will be executed alternately. In Partitioning phase we will split the data into each.. let me call these bucket. In Collecting phase we will collect the data again. Both phases will be executed for each position of the keys to be sorted. So the amount of cycles is based on the size of the keys (Let's rather say amount of digits if we for example want sort integers).

I don't want explain the 2 phases too much in detail because it would be too long and I hope you will read it till here because I don't know how to do this algorithm in-place..

Maybe you can explain with words instead of code? I need to know it for my exam but I couldn't find anything explaining on the internet, at least not in an easy, understandable way.

If you want me to explain more, please tell me. I will do anything to understand it.

eyesima
  • 279
  • 4
  • 13

1 Answers1

2

Wikipedia is (sometimes) your friend: https://en.wikipedia.org/wiki/Radix_sort#In-place_MSD_radix_sort_implementations.

I quote the article:

Binary MSD radix sort, also called binary quicksort, can be implemented in-place by splitting the input array into two bins - the 0s bin and the 1s bin. The 0s bin is grown from the beginning of the array, whereas the 1s bin is grown from the end of the array. [...] . The most significant bit of the first array element is examined. If this bit is a 1, then the first element is swapped with the element in front of the 1s bin boundary (the last element of the array), and the 1s bin is grown by one element by decrementing the 1s boundary array index. If this bit is a 0, then the first element remains at its current location, and the 0s bin is grown by one element. [...] . The 0s bin and the 1s bin are then sorted recursively based on the next bit of each array element. Recursive processing continues until the least significant bit has been used for sorting.

The main information is: it is a binary and recursive radix sort. In other words:

  • you have only two buckets, let's say 0 and 1, for each step. Since the algorithm is 'in- place' you swap elements (as in quicksort) to put each element in the right bucket (0 or 1), depending on its radix.

  • you process recursively: each bucket is split into two buckets, depending on the next radix.

It is very simple to understand for unsigned integers: you consider the bits from the most significant to the least significant. It may be more complex (and overkill) for other data types.

To summarize the differences with quicksort algorithm:

  • in quicksort, your choice of a pivot defines two "buckets": lower than pivot, greater than pivot.

  • in binary radix sort, the two buckets are defined by the radix (eg most significant bit).

In both cases, you swap elements to put each element in its "bucket" and process recursively.

jferard
  • 6,447
  • 1
  • 12
  • 26
  • thank you so much :) it sounds very similar to quicksort algorithm. i understood thank you again!! – eyesima Aug 17 '17 at 11:42