Insertion Sort | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Insertion Sort. Example : Suppose we need to sort the list
The sort works by 'inserting' each number in the correct position. It is easier to consider what happens in one pass of the insertion sort : Suppose we are at the following position - (three passes have already been done and the numbers 21,12, 32 and 17 have been 'inserted' in the list) :
The next number to be inserted is 19......remove it from the list and store in a temporary position....
Work back from the blank position. If the number is bigger than 19 then move it to the right..
and then insert number 19 into the blank position :
The next number to be inserted is 25...and so on. What would the list look like after the next pass?
Notes : The insertion sort is a fast sort - faster than the bubble sort for 'jumbled' files. Pseudocode : Assume the n records are held in an array Key[0..n], with Key[0] being used to hold the current key being looked at.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||