Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).

Similar presentations


Presentation on theme: "1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N)."— Presentation transcript:

1 1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).

2 2 7.6 Mergesort Merging of 2 sorted lists One pass through the input, if the output is put in a third list. A divide and conquer approach

3 3 7.6 Mergesort 1 13 24 26 2 15 27 38    Aptr Bptr Cptr 1 13 24 26 2 15 27 38 1    Aptr Bptr Cptr 1 13 24 26 2 15 27 38 1 2    Aptr Bptr Cptr

4 4 7.6 Mergesort 1 13 24 26 2 15 27 38 1 2 13    Aptr Bptr Cptr 1 13 24 26 2 15 27 38 1 2 13 15    Aptr Bptr Cptr 1 13 24 26 2 15 27 38 1 2 13 15 24    Aptr BptrCptr

5 5 7.6 Mergesort /* Figure 7.9 Mergesort Routine */ void MSort (ElementType A[ ], ElementType TmpArray [ ], int Left, int Right) {int Center; if (Left < Right) {Center = (Left + Right) / 2; MSort (A, TmpArray, Left, Center); MSort (A, TmpArray, Center + 1, Right); Merge (A, TmpArray, Left, Center + 1, Right); }}

6 6 7.6 Mergesort void Mergesort (ElementType A [ ], int N) {ElementType *TmpArray; TmpArray = malloc (N * sizeof (ElementType)); if (TmpArray != NULL) { MSort (A, TmpArray, 0, N - 1); free (TmpArray); } else FatalError ("No space for tmp array!!!");}

7 7 7.6 Mergesort /* Figure 7.10 Merge Routine */ /* Lpos = start of left half */ /* Rpos = start of right half */ void Merge (ElementType A [ ], ElementType TmpArray [ ], int Lpos, int Rpos, int RightEnd) { int i, LeftEnd, NumElements, TmpPos; LeftEnd = Rpos - 1; TmpPos = Lpos; NumElements = RightEnd - Lpos + 1;

8 8 7.6 Mergesort /* main loop */ while (Lpos <= LeftEnd && Rpos <= RightEnd) if (A [Lpos] <= A [Rpos]) TmpArray [TmpPos++] = A [Lpos++]; else TmpArray [TmpPos++] = A [Rpos++];

9 9 7.6 Mergesort while (Lpos <= LeftEnd) /* Copy rest of 1st half */ TmpArray [TmpPos++] = A [Lpos++]; while (Rpos <= RightEnd) /* Copy rest of 2nd half */ TmpArray [TmpPos++] = A [Rpos++]; /* Copy TmpArray back */ for (i = 0; i < NumElements; i++, RightEnd--) A [RightEnd] = TmpArray [RightEnd]; }

10 10 7.6 Mergesort Analysis of Mergesort Requires additional memory for TmpArray and work of copying to the temporary array and back. Used for external sorting.

11 11 7.7 Quicksort Fastest known sorting algorithm in practice Average running time is O (N logN) Worst case performance is O (N 2 ) Algorithm –If the number of elements in S is 0 or 1, then return. –Pick any element v in S. This is called the pivot.

12 12 7.7 Quicksort –Partition S -{v} (the remaining elements in S) into 2 disjoint groups: S 1 = {x  S-{v}|x  v}, and S 2 = {x  S-{v}|x  v}. –Return {quicksort (S 1 ) followed by v followed by quicksort (S 2 )}. More details –Set the pivot to the median among the first, center and last elements.

13 13 7.7 Quicksort –Exchange the last element with the pivot. –Set i at the first element. –Set j at the next-to-last element. –While i is on the left of j, move i right, skipping over elements that are smaller than the pivot. –Move j left, skipping over elements that are larger than the pivot. –When i and j have stopped, i is pointing at a large element and j at a small element.

14 14 7.7 Quicksort –If i is to the left of j, swap A [i] with A [j] and continue. –When i>j, swap the pivot element with the element at i. –All elements to the left of pivot are smaller than pivot, and all elements to the right of pivot are larger than pivot. What to do when some elements are equal to pivot?

15 15 7.7 Quicksort Example: 8 1 4 9 6 3 5 2 7 0 Start: 8 1 4 9 0 3 5 2 7 6 i j Move i: 8 1 4 9 0 3 5 2 7 6 i j Move j: 8 1 4 9 0 3 5 2 7 6 i j

16 16 7.7 Quicksort 1st swap: 2 1 4 9 0 3 5 8 7 6 i j Move i: 2 1 4 9 0 3 5 8 7 6 ij Move j: 2 1 4 9 0 3 5 8 7 6 i j

17 17 7.7 Quicksort 2nd swap: 2 1 4 5 0 3 9 8 7 6 i j Move i: 2 1 4 5 0 3 9 8 7 6 i&j Move j: 2 1 4 5 0 3 9 8 7 6 (i & j crossed) j i Swap element at i with pivot 2 1 4 5 0 3 6 8 7 9

18 18 7.7 Quicksort Does not perform well for small arrays; cutoff is about 10. When equal elements are encountered, it’s better to stop i and/or j (for swapping). Implementation –Sort A [Left], A [Right] and A [Center] in place. –Place the pivot at A [Right - 1]. –Initialize i = Left + 1, and j = Right - 2.

19 19 7.7 Quicksort /* Figure 7.12 Driver for quicksort */ void Quicksort (ElementType A [ ], int N) { Qsort( A, 0, N - 1 ); }

20 20 7.7 Quicksort /* Figure 7.13 Code to perform median-of-three partitioning */ /* Return median of Left, Center, and Right */ /* Order these and hide the pivot */ ElementType Median3 (ElementType A [ ], int Left, int Right) { int Center = (Left + Right) / 2; if (A [Left] > A [Center]) Swap (&A [Left], &A [Center]);

21 21 7.7 Quicksort if (A [Left] > A [Right]) Swap (&A [Left], &A [Right]); if (A [Center] > A [Right]) Swap (&A [Center], &A [Right]); /* Invariant: A [Left] <= A [Center] <= A [Right] */ Swap (&A [Center], &A [Right - 1]); return A [Right - 1]; /* Return pivot */ }

22 22 7.7 Quicksort /* Figure 7.14 Main quicksort routine */ #define Cutoff (3) void Qsort (ElementType A [ ], int Left, int Right) { int i, j; ElementType Pivot; if (Left + Cutoff <= Right) {Pivot = Median3 (A, Left, Right); i = Left; j = Right - 1;

23 23 7.7 Quicksort for ( ; ; ) {while (A [++i] < Pivot){ } while (A [--j] > Pivot){ } if (i < j) Swap (&A [i], &A [j]); else break; }

24 24 7.7 Quicksort /* Restore pivot */ Swap (&A [i], &A [Right - 1]); Qsort (A, Left, i - 1); Qsort (A, i + 1, Right); } else /* Do an insertion sort on the subarray */ InsertionSort (A + Left, Right - Left + 1); }

25 25 7.7 Quicksort Worst case of quicksort Best case T(N) = c N log N + N = O (N log N) Average case T(N) = O (N log N)


Download ppt "1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N)."

Similar presentations


Ads by Google