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

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

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

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

CS2420: Lecture 11

Vladimir KulyukinComputer Science Department

Utah State University

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

Outline

• Sorting Algorithms (Chapter 7)

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

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.

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

Quicksort

Array A

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

Quicksort

Array A

Pivot

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

Quicksort

Array A

Pivot

Left Right

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

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.

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

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.

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

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

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

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 donequickSort(A, left, i-1);quickSort(A, i+1, right);

}}

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

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?

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

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.

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

Definition

number.largest

2 theis numbers ofmedian A thN

N

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

Median-Of-Three Partitioning

Choose the pivot to be the median of the first, middle, and last elements of the array.

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

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

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

Partitioning: Example

8, 1, 4, 9, 6, 3, 5, 2, 7, 0

8, 1, 4, 9, 0, 3, 5, 2, 7, 6

i j

8, 1, 4, 9, 0, 3, 5, 2, 7, 6

i j

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

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

Partitioning: Example

2, 1, 4, 9, 0, 3, 5, 8, 7, 6

i j

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

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

Partitioning: Asymptotic Analysis

.Partition NON

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

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.

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

Quicksort: Observations

• Like MergeSort, Quicksort is a divide-and-conquer comparison sort.

• Unlike Mergesort, Quicksort does not allocate any extra arrays.

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

QuickSort: Recurrence

NLeftNTLeftTnT 1||||

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

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.

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

Worst-Case Analysis: Recurrence

2

1

nOnT

nnTnT