1 7.5 heapsort average number of comparison used to heapsort a random permutation of n items is 2n...

25
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).

Post on 21-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 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)

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).

Page 2: 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

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

Page 3: 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)

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

Page 4: 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)

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 Bptr Cptr

Page 5: 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)

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

Page 6: 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)

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!!!");}

Page 7: 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)

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;

Page 8: 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)

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++];

Page 9: 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)

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

Page 10: 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)

10

7.6 Mergesort

• Analysis of Mergesort

O(NlogN)NNlogNT(N)

N2)T(N/2T(N)

1T(1)

• Requires additional memory for TmpArray and work of copying to the temporary array and back.

• Used for external sorting.

Page 11: 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)

11

7.7 Quicksort

• Fastest known sorting algorithm in practice

• Average running time is O (N logN)

• Worst case performance is O (N2)

• 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.

Page 12: 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)

12

7.7 Quicksort– Partition S -{v} (the remaining elements in

S) into 2 disjoint groups:

S1 = {x S-{v}|x v}, and S2 = {x S-{v}|x v}.

– Return {quicksort (S1) followed by v followed by quicksort (S2)}.

• More details– Set the pivot to the median among the first,

center and last elements.

Page 13: 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)

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.

Page 14: 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)

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?

Page 15: 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)

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

Page 16: 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)

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

i j

Move j: 2 1 4 9 0 3 5 8 7 6

i j

Page 17: 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)

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

Page 18: 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)

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.

Page 19: 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)

19

7.7 Quicksort

/* Figure 7.12 Driver for quicksort */

void Quicksort (ElementType A [ ], int N)

{

Qsort( A, 0, N - 1 );

}

Page 20: 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)

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

Page 21: 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)

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 */

}

Page 22: 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)

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;

Page 23: 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)

23

7.7 Quicksort

for ( ; ; )

{while (A [++i] < Pivot){ }

while (A [--j] > Pivot){ }

if (i < j)

Swap (&A [i], &A [j]);

else

break;

}

Page 24: 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)

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

}

Page 25: 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)

25

7.7 Quicksort

• Worst case of quicksort

N

iNOicTNT

2

2)1()(

• Best caseT(N) = c N log N + N = O (N log N)

• Average caseT(N) = O (N log N)