Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.

Similar presentations


Presentation on theme: "CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University."— Presentation transcript:

1 CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University

2 Outline Sorting Algorithms (Chapter 7)

3 QuickSort: Sorting in Place N elements are partitioned into three segments: Left, Pivot, and Right. The Pivot segment contains exactly one element. No element in the Left segment is larger than the Pivot. No element in the Right segment is smaller than the Pivot.

4 Quicksort Array A

5 Quicksort Array A Pivot

6 Quicksort Array A Pivot LeftRight

7 Quicksort: Key Insight The keys to the left of the pivot are less than or equal to the pivot. The keys to the right of the pivot are greater than or equal to the pivot. Hence, the left and right segments can be sorted independently and no merge is required.

8 Quicksort: Example 13, 81, 92, 43, 65, 31, 57, 26, 75, 0 13, 0, 26, 43, 57, 31 65 92, 75, 81 Partition 65 is chosen to be the pivot.

9 Quicksort’s Correctness After the partition the pivot v will be in A[i]. The elements in sub-array L are sorted recursively. The elements in sub-array R are sorted recursively. No element in L is larger than v = A[i]. No element in R is smaller than v = A[i].

10 QuickSort: Pseudocode void quickSort(A, int left, int right) { if ( right – left + 1 <= 3 ) sort A directly; else { v = choose_pivot(A, left, right); i = partition(A, left, right, v); // i is the position of the pivot after // the partition is done quickSort(A, left, i-1); quickSort(A, i+1, right); }

11 Quicksort: 3 Basic Questions Q1: How do we choose the pivot? Q2: How do we perform the partition? Q3: How do we treat elements equal to the pivot?

12 Q1: Picking a Pivot We should not pick a pivot near the front or end of the array. The middle element is a reasonable, but passive choice. Median-of-three partitioning is a reliable choice in practice.

13 Definition

14 Median-Of-Three Partitioning Choose the pivot to be the median of the first, middle, and last elements of the array.

15 Q2: Partitioning Strategy Swap the pivot v with the last element in the array. Run index i from left to right. Run index j from right to left. When A[i] >= v, stop incrementing i. When A[j] <= v, stop decrementing j. If i and j have not crossed, swap(A[i], A[j]) and continue. If i and j have crossed, swap the pivot with A[i].

16 Partitioning: Example 8, 1, 4, 9, 6, 3, 5, 2, 7, 0 8, 1, 4, 9, 0, 3, 5, 2, 7, 6 i j ij A[i] >= 6 so i is stopped. A[j] > 6 so j can be decremented. A[j] <= 6 so j can be stopped. Now we can swap A[i] and A[j].

17 Partitioning: Example 2, 1, 4, 9, 0, 3, 5, 8, 7, 6 ij 2, 1, 4, 5, 0, 3, 9, 8, 7, 6 ij 2, 1, 4, 5, 0, 3, 6, 8, 7, 9 i and j crossed, so we stop. Finally we swap the pivot with A[i].

18 Partitioning: Asymptotic Analysis

19 Q3: Equal Elements Elements equal to the pivot are swapped. This seems to be redundant, but turns out to be the best choice in practice since we do not have to deal with a number of special cases. For example, we do not have to change our algorithm for arrays of identical elements.

20 Quicksort: Observations Like MergeSort, Quicksort is a divide-and- conquer comparison sort. Unlike Mergesort, Quicksort does not allocate any extra arrays.

21 QuickSort: Recurrence

22 Worst-Case Analysis In the worst case, we always pick the pivot as the smallest or largest element in the array. In this case, we break the basic recursive design principle to partition the problem into chucks of equal size. The array is reduced only by 1 element.

23 Worst-Case Analysis: Recurrence


Download ppt "CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University."

Similar presentations


Ads by Google