Apply Quicksort to the list M,E,R,G,E,S,O,R,T

Using ^ below a letter to indicate it’s a pivot, and * to indicate it’s already been pivoted. If a partition has odd length, I pick the “rounded down middle” (partition length 4 would have the 2nd item as pivot).

Note how in the 4th line, there are two R‘s, with one of them being the pivot. In cases like this, the duplicate (the other R) can go on either side.

Edit: when comparing two letters, you need to sort them based on their position in the alphabet. If you “sort” based on their relative positions, you’re never going to move any elements so naturally you’ll never sort the list.

Edit #2: The algorithm I’m using is, unsurprisingly, quicksort.

MERGESORT -> EEMRGSORT
    ^         ^

EEMRGSORT -> EEMRGORST
^*   ^       ^*     ^

EEMRGORST -> EEGMRORST
**  ^  *^    **^    **

EEGMRORST -> EEGMORRST
*** ^  **    ***  ^ **

EEGMORRST -> EEGMORRST
***^ *^**    ***^ *^**

EEGMORRST -> EEGMORRST
****^****    ****^****

EEGMORRST -> Complete
*********

If I’ve made a mistake, please correct me.

Leave a Comment