Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Analysis Lakshmish Ramaswamy. Insertion Sort Conceptual Logic Create a duplicate array Insert elements from original array into duplicate array.

Similar presentations


Presentation on theme: "Algorithm Analysis Lakshmish Ramaswamy. Insertion Sort Conceptual Logic Create a duplicate array Insert elements from original array into duplicate array."— Presentation transcript:

1 Algorithm Analysis Lakshmish Ramaswamy

2 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

3 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]

4 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; }

5 Trace [9, 3, 8, 12, 1, 5, 22, 18, 14, 2]

6 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]

7 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(2 + 3 + ….+ N) = c/2*(N 2 -N- 2) O(N 2 ) algorithm

8 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

9 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]); }

10 Trace [9, 3, 8, 12, 1, 5, 22, 18, 14, 2]

11 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

12 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

13 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); }

14 Trace [1,3, 8, 9, 12] [2, 5, 14, 18, 22]

15 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

16 Trace [9, 3, 8, 12, 1, 5, 22, 18]


Download ppt "Algorithm Analysis Lakshmish Ramaswamy. Insertion Sort Conceptual Logic Create a duplicate array Insert elements from original array into duplicate array."

Similar presentations


Ads by Google