Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Insertion sort [ 2 25 34 4 -2 99 16 50 100 ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ 2 4 25 34 -2 99 16 50 100 ] i=3 j=1 insert i=4 at j=0 [ -2 2 4 25 34.

Similar presentations


Presentation on theme: "1 Insertion sort [ 2 25 34 4 -2 99 16 50 100 ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ 2 4 25 34 -2 99 16 50 100 ] i=3 j=1 insert i=4 at j=0 [ -2 2 4 25 34."— Presentation transcript:

1 1 Insertion sort [ 2 25 34 4 -2 99 16 50 100 ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ 2 4 25 34 -2 99 16 50 100 ] i=3 j=1 insert i=4 at j=0 [ -2 2 4 25 34 99 16 50 100 ] i=4 j=0 i=5 j=5 insert i=6 at j=3 [ -2 2 4 16 25 34 99 50 100 ] i=6 j=3 insert i=7 at j=6 [ -2 2 4 16 25 34 50 99 100 ] i=7 j=6 [ -2 2 4 16 25 34 50 99 100 ]

2 2 Insertion sort class insertionsort { public static void main (String arg[]) { int m [] = {2, 25, 34, 4, -2, 99, 16, 50, 100}; printArray (m); Sort (m); printArray (m); }

3 3 Insertion sort public static void Sort (int array[]) { int i, j; for (i=1; i<array.length; i++) { for (j=0; array[i] > array[j]; j++); if (j < i) { System.out.println("insert i="+i+" at j="+j); Insert (array, i, j); } System.out.println("i="+i+" j="+j); // Invariant: the first i+1 element are sorted }

4 4 Insertion sort public static void Insert (int array[], int oldpos, int newpos) { int i; int candidate = array[oldpos]; for (i=oldpos-1; i>=newpos; i--) { array[i+1] = array[i]; } array[i+1] = candidate; }

5 5 Insertion sort public static void printArray(int a[]){ System.out.print("[ "); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println("]"); } } // end class

6 6 Insertion sort How many comparisons? –Need to consider worst case –What does the array look like in the worst case? –What is the best case? Just the number of comparisons does not tell you the whole story –How many assignments do you execute?

7 7 Merging two sorted arrays Suppose we have sorted the first m and the remaining n-m elements of an array separately We need to merge these two sorted halves to get a complete sorted array Assume that everything is sorted in ascending order

8 8 Merging two sorted arrays public static void Merge (double array[], int start, int m, int n) { // start would be 0 in this case double temp[] = new double[n-start]; int index = 0, index1, index2, i; for (index1=start, index2=m; (index1 < m) && (index2 < n);) { if (array[index1] < array[index2]) { temp[index] = array[index1]; index++; index1++; } else { temp[index] = array[index2]; index++; index2++; } } // continued in next slide

9 9 Merging two sorted arrays for(;index1<m;index1++,index++) { temp[index] = array[index1]; } for(;index2<n;index2++,index++) { temp[index] = array[index2]; } for (i=start;i<n;i++) { array[i] = temp[i-start]; }


Download ppt "1 Insertion sort [ 2 25 34 4 -2 99 16 50 100 ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ 2 4 25 34 -2 99 16 50 100 ] i=3 j=1 insert i=4 at j=0 [ -2 2 4 25 34."

Similar presentations


Ads by Google