1 Chapter 7 Sorting Sorting of an array of N items A [0], A [1], A [2], …, A [N-1] Sorting in ascending order Sorting in main memory (internal sort)
2 7.2 Insertion Sort N - 1 passes (for P = 1 to N - 1) In pass P, given that A [0] to A [P-1] have been sorted, determine where to insert A [P].
3 7.2 Insertion Sort Let Tmp = A [P] For J = P to 1 –compare A [J-1] with Tmp –if A [J-1] > Tmp right shift A [J-1] else set A [J] = Tmp end of pass P Insertion sort routine in Figure 7.2
4 7.2 Insertion Sort Example Original Moved After p= After p= After p= After p= After p=
5 7.3 Analysis of Insertion Sort Maximum number of comparisons … + N-1 = N (N-1) / 2 An inversion in an array of numbers is any ordered pair (i, j) having the property that i A [j]. There are 9 inversions in the original list in the example. On average, there are N(N-1)/4 inversions in an array of N distinct elements.
6 7.3 Insertion Sort Each swap between 2 adjacent elements removes exactly one inversion. On average, an algorithm that sorts by exchanging adjacent elements requires N(N-1)/4 swaps. A sorting algorithm that is subquadratic must do comparisons and exchanges between elements that are far apart.
7 7.4 Shellsort Break the quadratic time barrier by comparing elements that are distant. The distance between comparisons decreases as the algorithm runs until the last phase, in which adjacent elements are compared (diminishing increment sort). Increment sequences h 1, h 2, h 3,..., h t, used in reverse order; h 1 =1.
8 7.4 Shellsort After a phase, with an increment h k, A [i] <= A [i + h k ]. All elements spaced h k apart are sorted. Example Original sorted sorted sorted
9 7.4 Shellsort /* Figure 7.4 Shellsort routine */ /* using increments of N/2, N/4,..., 1 */ void Shellsort (ElementType A [ ], int N) { int i, j, Increment; ElementType Tmp; for (Increment = N / 2; Increment > 0; Increment /= 2 ) for (i = Increment; i < N; i++ )
Shellsort { Tmp = A [i]; for (j = i; j >= Increment; j -= Increment) if (Tmp < A [j - Increment]) A [j] = A [j - Increment]; else break; A [j] = Tmp; }
Shellsort Hibbard’s increment 1, 3, 7,.., 2 k -1 (consecutive increments have no common factors); worst-case running time O (N 3/2 ), average O (N 5/4 ) Sedgewicks increment 1, 5, 19, 41, 109,... (each term is either 9*4 i -9*2 i +1 or 4 i - 3*2 i +1); worst-case running time O (N 4/3 ), average O (N 7/6 )
Heapsort First build a binary (max)heap of N elements. Then perform N-1 DeleteMax operations by swapping the last element in the heap with the first, decrementing the heap size, and percolating down. Sorting in O (N log N) time, but in practice, is slower than Shellsort that uses Sedgewick’s increment sequence.
Heapsort Figure 7.6 (Max)heap after BuildHeap
Heapsort Figure 7.7 Heap after first DeleteMax
Heapsort /* Figure 7.8 Heapsort */ /* index starts from 0 */ #define LeftChild (i) (2*(i)+1) void PercDown (ElementType A [], int i, int N) {int Child; ElementType Tmp; for (Tmp = A [i]; LeftChild (i) < N; i = Child) { Child = LeftChild (i);
Heapsort if (Child != N - 1 && A [Child + 1] > A [Child]) Child++; if (Tmp < A [Child]) A [i] = A [Child]; else break; } A [i] =Tmp; }
Heapsort void Heapsort (ElementType A [ ], int N) { int i; for (i = N / 2; i >= 0; i--) /* BuildHeap */ PercDown (A, i, N); for (i = N - 1; i > 0; i--) { Swap (&A [0], &A[i]); /* DeleteMax */ PercDown (A, 0, i); }}