After learning common sorting algorithms, feel like demonstrating them using not only brief description but also animated demos.
Here contains 6 sorting algorithms, half are simple, half are advanced:
This maybe the simplest sort, notoriously slow though.
Suppose we were arranging a queue of baseball players by their height.
Start from leftmost.
compare two players
if the one on the left is taller, swap them. Otherwise, no action.
move one position right
Start from leftmost too.
look for shortest player from current position to right
swap shortest player with the player at current position
move one position right
In most cases, this is the best of elementary sorts. It's about twice as fast as the bubble sort.
The steps is somewhat complicated than sorts above.Start from leftmost.
partially sort left players
choose the first unsorted player as marked player
shift the players shorter than marked player to right
insert marked player into the previous position of first shifted player.
The heart of the merge sort algorithm are the merging of two already-sorted arrays and recursion.
As shown in picture, main steps are:
Recur to split numbers into 2 parts
merge 2 parts
The name "Shell sort" is named for Donald Shell, who discovered it. It's based on insertion sort, but adds a new feature that dramatically improves the insertion sort's performance.
Main steps
divide array into groups by interval(3 for example) and sort them continously until all items are divided and sorted.
diminish the interval and continue to divide and sort until the interval becomes 1.
In the majority of situations, this is the fastest sort.
choose a pivot(rightmost item)
partition the array into left sub array(smaller keys) and right sub array(larger keys)
recur step2 to left sub array and right sub array
Thanks for your reading. Welcome to subscribe my blog by Github.