Insertion Sort
Learning Objectives Review the Bubble and Selection Sorts Understand the speed, stability and how the insertion sort works
Review Show after each pass of the Bubble Sort Low High 10 6 20 15 90 12 14 Show after each pass of the Selection Sort
Insertion Sort Can you describe how it works? Animation http://maven.smith.edu/~thiebaut/java/sort/demo.html
Insertion Sort Details Speed Stability How it works Your turn Low High 20 10 8 12 31 9 15
Procedure insertion(var theArray:theArrayType; size:integer); dummy:integer; pass, check:integer; Begin for pass:= 2 to size do begin dummy:= theArray[pass]; check := pass-1; while (theArray[check]>dummy) and (check>=1) do theArray[check+1] := theArray[check]; check:=check-1; end; theArray[check+1] := dummy; End;
Program Options Sort Race Name, heights Sort Randomly generate 50000 integers (from 1-100) Time how long it takes the insertion sort to put the values in order. (Use the gettime() procedure) Push: Time sorting the same values with all of the sorts and rate their speed. Name, heights Sort Input: 10 names and heights Output: The names and corresponding heights in a chart sorted by height using an Insertion sort. Push: Let the user decide if they want to sort by height or name, then sort and show the chart. Push: Input an unknown number of names and heights to sort.