Algorithm Analysis Lakshmish Ramaswamy
Insertion Sort Conceptual Logic Create a duplicate array Insert elements from original array into duplicate array (i th element in iteration i) Maintain sortedness of duplicate array at all times –May need to move existing elements to accommodate the incoming element
Trace [9, 3, 8, 12, 1, 5, 22, 18, 14, 2] [9] [3, 9] [3, 8, 9] [3, 8, 9, 12] [1, 3, 8, 9, 12] … [1, 2, 3, 5, 8, 9, 12, 14, 18, 22]
Insertion Sort Algorithm public static void InsertionSort(int[] Arr){ int i, j, k, currElement; if(Arr.length == 1) return; for (i = 1; i < (Arr.length); i++){ currElement = Arr[i]; for(j = 0; j < i && Arr[j] <= currElement; j++); for(k = (i-1); k => j; k++) Arr[k+1] = Arr[k]; Arr[j] = currElement; } return; }
Trace [9, 3, 8, 12, 1, 5, 22, 18, 14, 2]
Trace [9, 3, 8, 12, 1, 5, 22, 18, 14, 2] [3, 9, 8, 12, 1, 5, 22, 18, 14, 2] [3, 8, 9, 12, 1, 5, 22, 18, 14, 2] [1, 3, 8, 9, 12, 5, 22, 18, 14, 2] [1, 3, 5, 8, 9, 12, 22, 18, 14, 2] [1, 3, 5, 8, 9, 12, 18, 22, 14, 2] [1, 3, 5, 8, 9, 12, 14, 18, 22, 2] [1, 2, 3, 5, 8, 9, 12, 14, 18, 22]
Analysis Constant cost for comparison and assignment –Assume equal cost ‘c’ for simplicity For i th iteration –‘t’ comparisons for some t <= (i-1) –i-t+1 assignments –Cost = c(i+1) Total cost = c( ….+ N) = c/2*(N 2 -N- 2) O(N 2 ) algorithm
Bubble Sort Imitates bubbles raising through water Compares two neighboring elements –Swap if they are in the wrong order Repeat the procedure (N-1) times
Bubble Sort Algorithm public static void BubbleSort(int[] Arr){ int i, j; for(i = 0; i < (Arr.length-1); i++){ for(j = (Arr.length-1); j > i; j--){ if(Arr[j] < Arr[j-1]) swap(Arr[j], Arr[j-1]); }
Trace [9, 3, 8, 12, 1, 5, 22, 18, 14, 2]
Analysis Constant time for comparison and swap Outer loop is executed (N-1) times For a particular i the inner loop is executed N-i-1 times # comparisons = (N-1) + (N-2) + … + 2 –N(N-1)/2 – 1 O(N 2 ) algorithm
Merge Problem – Merge two sorted arrays such that the resultant array remains sorted Logic –Keep pointers to both arrays –Compare the current pointer elements of two arrays –Place the one that is lowest and increment its pointer
Merge Algorithm public static int[] Merge(int[] A1, int[] A2){ int[] A3 = new int[A1.length+ A2.length]; int i, j; while(i < A1.length && j < A2.length){ if(i => A1.length){ A3[i+j] = A2[j]; j++; } elseif (j => A2.length){ A3[i+j] = A1[i]; i++; } elseif(A1[i] < A2[j]){ A3[i+j] = A1[i]; i++; } else{ A3[i+j] = A2[j]; j++; } } return(A3); }
Trace [1,3, 8, 9, 12] [2, 5, 14, 18, 22]
Merge Sort Recursive algorithm Partition array at the center Call merge sort on each part of the array Call merge to combine the two parts
Trace [9, 3, 8, 12, 1, 5, 22, 18]