Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1 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 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 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 4 7.2 Insertion Sort Example Original 34 8 64 51 32 21 Moved After p=1 8 34 64 51 32 21 1 After p=2 8 34 64 51 32 21 0 After p=3 8 34 51 64 32 21 1 After p=4 8 32 34 51 64 21 3 After p=5 8 21 32 34 51 64 4

5 5 7.3 Analysis of Insertion Sort Maximum number of comparisons 1 + 2 + 3 + … + 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 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 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 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 81 94 11 96 12 35 17 95 28 58 41 75 15 5-sorted 35 17 11 28 12 41 75 15 96 58 81 94 95 3-sorted 28 12 11 35 15 41 58 17 94 75 81 96 95 1-sorted 11 12 15 17 28 35 41 58 75 81 94 95 96

9 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++ )

10 10 7.4 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; }

11 11 7.4 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 )

12 12 7.5 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.

13 13 7.5 Heapsort Figure 7.6 (Max)heap after BuildHeap

14 14 7.5 Heapsort Figure 7.7 Heap after first DeleteMax

15 15 7.5 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);

16 16 7.5 Heapsort if (Child != N - 1 && A [Child + 1] > A [Child]) Child++; if (Tmp < A [Child]) A [i] = A [Child]; else break; } A [i] =Tmp; }

17 17 7.5 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); }}


Download ppt "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)"

Similar presentations


Ads by Google