Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9
Insertion Sort
while some elements unsorted:
Using linear search, find the location in the sorted portion where the 1st element of the unsorted portion should be inserted Move all the elements after the insertion location up one position to make space for the new element 45
An insertion sort partitions the array into two regions An insertion sort of an array of five integers Insertion Sort Algorithm public void insertionSort(Comparable[] arr) { for (int i = 1; i < arr.length; ++i) { Comparable temp = arr[i]; int pos = i; // Shuffle up all sorted items > arr[i] while (pos > 0 && arr[pos-1]>temp) { arr[pos] = arr[pos–1]; pos--; } // end while // Insert the current item arr[pos] = temp; } } Insertion Sort Analysis public void insertionSort(Comparable[] arr) { for (int i = 1; i < arr.length; ++i) { outer loop Comparable temp = arr[i]; outer times int pos = i; // Shuffle up all sorted items > arr[i] while (pos > 0 && inner loop arr[pos-1].compareTo(temp) > 0) { arr[pos] = arr[pos–1]; pos--; inner times } // end while // Insert the current item arr[pos] = temp; } } Insertion Sort: Number of Comparisons # of Sorted Best case Worst case Elements 0 0 0 1 1 1 2 1 2 … … … n-1 1 n-1 n-1 n(n-1)/2
Remark: we only count comparisons of elements in the array.
Insertion Sort: Cost Function 1 operation to initialize the outer loop The outer loop is evaluated n-1 times 5 instructions (including outer loop comparison and increment) Total cost of the outer loop: 5(n-1) How many times the inner loop is evaluated is affected by the state of the array to be sorted Best case: the array is already completely sorted so no “shifting” of array elements is required. We only test the condition of the inner loop once (2 operations = 1 comparison + 1 element comparison), and the body is never executed Requires 2(n-1) operations. Insertion Sort: Cost Function Worst case: the array is sorted in reverse order (so each item has to be moved to the front of the array) In the i-th iteration of the outer loop, the inner loop will perform 4i+1 operations Therefore, the total cost of the inner loop will be 2n(n-1)+n-1 Time cost: Best case: 7(n-1) Worst case: 5(n-1)+2n(n-1)+n-1
What about the number of moves?
Best case: 2(n-1) moves Worst case: 2(n-1)+n(n-1)/2 Insertion Sort: Average Case Is it closer to the best case (n comparisons)? The worst case (n * (n-1) / 2) comparisons? It turns out that when random data is sorted, insertion sort is usually closer to the worst case Around n * (n-1) / 4 comparisons Calculating the average number of comparisons more exactly would require us to state assumptions about what the “average” input data set looked like This would, for example, necessitate discussion of how items were distributed over the array Exact calculation of the number of operations required to perform even simple algorithms can be challenging (for instance, assume that each initial order of elements has the same probability to occur)