Top Banner
CMSC 132: Object-Oriented Programming II Sorting CMSC 132 Summer 2017 1
123

CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Jun 06, 2018

Download

Documents

vudien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

CMSC 132: Object-Oriented Programming II

Sorting

CMSC 132 Summer 2017 1

Page 2: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

What Is Sorting?

• To arrange a collection of items in some specified order.• Numerical order • Lexicographical order

• Input: sequence <a1, a2, …, an> of numbers.•Output: permutation <a'1, a'2, …, a’n> such that a'1 £ a'2£ … £ a'n .

• Example• Start à 1 23 2 56 9 8 10 100• End à 1 2 8 9 10 23 56 100

CMSC 132 Summer 2017 2

Page 3: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Why Sort?

• A classic problem in computer science.

• Data requested in sorted order• e.g., list students in increasing GPA order

• Searching

• To find an element in an array of a million elements• Linear search: average 500,000 comparisons• Binary search: worst case 20 comparisons

• Database, Phone book• Eliminating duplicate copies in a collection of records• Finding a missing element, Max, Min

CMSC 132 Summer 2017 3

Page 4: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Sorting Algorithms

• Selection Sort Bubble Sort• Insertion Sort Shell Sort• T(n)=O(n2) Quadratic growth• In clock time

• Double input -> 4X time• Feasible for small inputs, quickly unmanagable

• Halve input -> 1/4 time• Hmm... can recursion save the day?• If have two sorted halves, how to produce sorted full

result?

10,000 3 sec 20,000 17 sec

50,000 77 sec 100,000 5 min

CMSC 132 Summer 2017 4

Page 5: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Divide and Conquer

1. Base case: the problem is small enough, solve directly

2. Divide the problem into two or more similar and smallersubproblems

3. Recursively solve the subproblems

4. Combine solutions to the subproblems

CMSC 132 Summer 2017 5

Page 6: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge Sort

• Divide and conquer algorithm• Worst case: O(nlogn)• Stable

• maintain the relative order of records with equal values

• Input: 12, 5, 8, 13, 8, 27 • Stable: 5, 8, 8, 12, 13, 27 • Not Stable:5, 8, 8, 12, 13, 27

CMSC 132 Summer 2017 6

Page 7: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge Sort: Idea

MergeRecursively sort

Divide intotwo halves

FirstPart SecondPart

FirstPart SecondPart

A:

A is sorted!

CMSC 132 Summer 2017 7

Page 8: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge-Sort: Merge

Sorted Sorted

merge

A:

L: R:

Sorted

CMSC 132 Summer 2017 8

Page 9: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge Example

L: R:1 2 6 8 3 4 5 7

A:

CMSC 132 Summer 2017 9

i=0 j=0

Page 10: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge Example

L: R:

1

1 2 6 8 3 4 5 7

A:

CMSC 132 Summer 2017 10

i=1 j=0

Page 11: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge Example

L: R:

21

1 2 6 8 3 4 5 7

A:

CMSC 132 Summer 2017 11

i=2 j=0

Page 12: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge Example cont.

1 2 3 4 5 6 7 8

L:

A:

3 5 15 28 6 10 14 22

R:1 2 6 8 3 4 5 7

i=4 j=4

k=8

CMSC 132 Summer 2017 12

Page 13: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort algorithm

MERGE-SORT A[1 . . n]

1. If n = 1, done.2. Recursively sort A[ 1 . . én/2ù ] and A[

én/2ù+1 . . n ] .3. “Merge” the 2 sorted lists.

Key subroutine: MERGE

CMSC 132 Summer 2017 13

Page 14: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 14

Page 15: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 15

Page 16: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 16

Page 17: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 17

Page 18: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 18

Page 19: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 19

Page 20: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 20

Page 21: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 21

Page 22: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 22

Page 23: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 23

Page 24: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 24

Page 25: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 25

Page 26: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 26

Page 27: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 27

Page 28: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 28

Page 29: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 29

Page 30: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 30

Page 31: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 31

Page 32: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 32

Page 33: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort (Example)

CMSC 132 Summer 2017 33

Page 34: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Analysis of merge sort

MERGE-SORT A[1 . . n]

1. If n = 1, done.2. Recursively sort A[ 1 . . én/2ù ]

and A[ én/2ù+1 . . n ] .3. “Merge” the 2 sorted lists

T(n)

Q(1)

2T(n/2)

Q(n)

CMSC 132 Summer 2017 34

Page 35: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Analyzing merge sort

T(n) =Q(1) if n = 1;

2T(n/2) + Q(n) if n > 1.

T(n) = Q(n lg n) (n > 1)

CMSC 132 Summer 2017 35

Page 36: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Q(1)

h = log n

cn

cn

cn

#leaves = n Q(n)

Total = Q(n log n)

CMSC 132 Summer 2017 36

Page 37: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Memory Requirement

Needs additional n locations because it is difficult to merge two sorted sets in place

L: R:1 2 6 8 3 4 5 7

A:

CMSC 132 Summer 2017 37

Page 38: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge Sort Conclusion

• Merge Sort: O(n log n)

• asymptotically beats insertion sort in the worst case

• In practice, merge sort beats insertion sort for n > 30 or so

• Space requirement:

• O(n), not in-place

CMSC 132 Summer 2017 38

Page 39: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

HEAPSORT

CMSC 132 Summer 2017 39

Page 40: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Heapsort

• Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations

• Heapsort does not require any additional storage• As its name implies, heapsort uses a heap to store the

array

CMSC 132 Summer 2017 40

Page 41: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Heapsort Algorithm

• When used as a priority queue, a heap maintains a smallest value at the top

• The following algorithm • places an array's data into a heap, • then removes each heap item (O(n log n)) and moves it back into

the array• This version of the algorithm requires n extra storage

locationsHeapsort Algorithm: First Version

1. Insert each value from the array to be sorted into a priority queue (heap).2. Set i to 03. while the priority queue is not empty4. Remove an item from the queue and insert it back into the array at position i5. Increment i

CMSC 132 Summer 2017 41

Page 42: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort

89

76 74

37 32 39 66

20 26 18 28 29 6

CMSC 132 Summer 2017 42

Page 43: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

89

76 74

37 32 39 66

20 26 18 28 29 6

CMSC 132 Summer 2017 43

Page 44: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

89

76 74

37 32 39 66

20 26 18 28 29 6

CMSC 132 Summer 2017 44

Page 45: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

6

76 74

37 32 39 66

20 26 18 28 29 89

CMSC 132 Summer 2017 45

Page 46: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

76

6 74

37 32 39 66

20 26 18 28 29 89

CMSC 132 Summer 2017 46

Page 47: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

76

37 74

6 32 39 66

20 26 18 28 29 89

CMSC 132 Summer 2017 47

Page 48: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

CMSC 132 Summer 2017 48

Page 49: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

CMSC 132 Summer 2017 49

Page 50: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

CMSC 132 Summer 2017 50

Page 51: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

76

37 74

26 32 39 66

20 6 18 28 29 89

CMSC 132 Summer 2017 51

Page 52: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

29

37 74

26 32 39 66

20 6 18 28 76 89

CMSC 132 Summer 2017 52

Page 53: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

74

37 29

26 32 39 66

20 6 18 28 76 89

CMSC 132 Summer 2017 53

Page 54: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

CMSC 132 Summer 2017 54

Page 55: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

CMSC 132 Summer 2017 55

Page 56: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

CMSC 132 Summer 2017 56

Page 57: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

74

37 66

26 32 39 29

20 6 18 28 76 89

CMSC 132 Summer 2017 57

Page 58: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

28

37 66

26 32 39 29

20 6 18 74 76 89

CMSC 132 Summer 2017 58

Page 59: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Heapsort (cont.)

6

18 20

26 28 29 32

37 39 66 74 76 89

CMSC 132 Summer 2017 59

Continue until everything sorted

Page 60: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Revising the Heapsort Algorithm

• If we implement the heap as an array• each element

removed will be placed at the end of the array, and

• the heap part of the array decreases by one element

CMSC 132 Summer 2017 60

Page 61: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Algorithm for In-Place Heapsort

1. Build a heap by rearranging the elements in an unsorted array

2. While the heap is not empty

3. Remove the first item from the heap by swapping it with the last item in the heap and restoring the heap property

CMSC 132 Summer 2017 61

Page 62: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Analysis of Heapsort

• Because a heap is a complete binary tree, it has log n levels

• Building a heap of size n requires finding the correct location for an item in a heap with log n levels

• Each insert (or remove) is O(log n)• With n items, building a heap is O(n log n)• No extra storage is needed

CMSC 132 Summer 2017 62

Page 63: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

QUICKSORT

CMSC 132 Summer 2017 63

Page 64: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Quicksort

• Developed in 1962• Quicksort selects a specific value called a pivot and

rearranges the array into two parts (called partioning)• all the elements in the left subarray are less than or

equal to the pivot• all the elements in the right subarray are larger than

the pivot• The pivot is placed between the two subarrays

• The process is repeated until the array is sorted

CMSC 132 Summer 2017 64

Page 65: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort vs Quick Sort

CMSC 132 Summer 2017 65

13 89 46 22 57 76 98 34 66 83

13 89 46 22 57 76 98 34 66 83

split

13 22 46 57 89 76 98 34 66 83

sort recursively

13 22 34 46 57 66 76 83 89 98

merge

Merge sort

Page 66: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Merge sort vs Quick Sort

CMSC 132 Summer 2017 66

13 89 46 22 57 76 98 34 66 83

13 46 22 57 34 89 76 98 66 83

Split (smart, extra work here)

13 22 34 46 57 66 76 83 89 98

sort recursively

Merge is not necessary

Page 67: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort

44 75 23 43 55 12 64 77 33

CMSC 132 Summer 2017 67

Page 68: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

44 75 23 43 55 12 64 77 33

Arbitrarily select the first element

as the pivot

CMSC 132 Summer 2017 68

Page 69: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

55 75 23 43 44 12 64 77 33

Swap the pivot with the element in the

middle

CMSC 132 Summer 2017 69

Page 70: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

55 75 23 43 44 12 64 77 33

Partition the elements so that all values less than or equal to the pivot are to the left, and all values greater than the pivot

are to the right

CMSC 132 Summer 2017 70

Page 71: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Partition the elements so that all values less than or equal to the

pivot are to the left, and all values greater than the pivot are

to the right

CMSC 132 Summer 2017 71

Page 72: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Quicksort Example(cont.)

12 33 23 43 44 55 64 77 75

44 is now in its correct position

CMSC 132 Summer 2017 72

Page 73: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Now apply quicksort recursively to the two

subarrays

CMSC 132 Summer 2017 73

Page 74: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Pivot value = 12

CMSC 132 Summer 2017 74

Page 75: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Pivot value = 12

CMSC 132 Summer 2017 75

Page 76: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 33 23 43 44 55 64 77 75

Pivot value = 33

CMSC 132 Summer 2017 76

Page 77: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

CMSC 132 Summer 2017 77

Page 78: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

CMSC 132 Summer 2017 78

Page 79: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

Left and right subarrays have single values;

they are sorted

CMSC 132 Summer 2017 79

Page 80: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 33

Left and right subarrays have single values; they

are sorted

CMSC 132 Summer 2017 80

Page 81: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 55

CMSC 132 Summer 2017 81

Page 82: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

Pivot value = 64

12 23 33 43 44 55 64 77 75

CMSC 132 Summer 2017 82

Page 83: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 77 75

Pivot value = 77

CMSC 132 Summer 2017 83

Page 84: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

Pivot value = 77

CMSC 132 Summer 2017 84

Page 85: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

Pivot value = 77

CMSC 132 Summer 2017 85

Page 86: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

Left subarray has single value; it is

sorted

CMSC 132 Summer 2017 86

Page 87: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Quicksort (cont.)

12 23 33 43 44 55 64 75 77

CMSC 132 Summer 2017 87

Page 88: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Quick Sort Algorithm

CMSC 132 Summer 2017 88

/* quicksort the subarray from a[lo] to a[hi] */

void sort(Comparable[] a, int lo, int hi) {if (hi <= lo) return;int j = partition(a, lo, hi);sort(a, lo, j-1);sort(a, j+1, hi);

}

Page 89: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Partition

CMSC 132 Summer 2017 89

// partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi] // and return the index j. int partition(Comparable[] a, int lo, int hi) {

int i = lo;int j = hi + 1;Comparable v = a[lo];while (true) {

// find item on lo to swapwhile (less(a[++i], v))

if (i == hi) break;/* find item on hi to swap */while (less(v, a[--j]))if (j == lo) break;

// check if pointers crossif (i >= j) break;exch(a, i, j);

} // put partitioning item v at a[j]exch(a, lo, j);

// now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi] return j;

}

Page 90: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Analysis of Quicksort

• If the pivot value is a random value selected from the current subarray,• then statistically half of the items in the subarray will

be less than the pivot and half will be greater• If both subarrays have the same number of elements

(best case), there will be log n levels of recursion• At each recursion level, the partitioning process involves

moving every element to its correct position—n moves• Quicksort is O(n log n), just like merge sort

CMSC 132 Summer 2017 90

Page 91: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Analysis of Quicksort (cont.)

• The array split may not be the best case, i.e. 50-50

• An exact analysis is difficult (and beyond the scope of this class), but, the running time will be bounded by a constant x n log n

CMSC 132 Summer 2017 91

Page 92: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Analysis of Quicksort (cont.)

• A quicksort will give very poor behavior if, each time the array is partitioned, a subarray is empty.

• In that case, the sort will be O(n2)

• Under these circumstances, the overhead of recursive calls and the extra run-time stack storage required by these calls makes this version of quicksort a poor performer relative to the quadratic sorts.

CMSC 132 Summer 2017 92

Page 93: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Algorithm for Partitioning

44 75 23 43 55 12 64 77 33

If the array is randomly ordered, it does not matter which element

is the pivot.

For simplicity we pick the element with subscript first

CMSC 132 Summer 2017 93

Page 94: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

44 75 23 43 55 12 64 77 33

If the array is randomly ordered, it does not matter which element

is the pivot.

For simplicity we pick the element with subscript first

CMSC 132 Summer 2017 94

Page 95: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

44 75 23 43 55 12 64 77 33

For visualization purposes, items less than or equal to the pivot will be colored blue; items

greater than the pivot will be colored light purple

CMSC 132 Summer 2017 95

Page 96: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

44 75 23 43 55 12 64 77 33

For visualization purposes, items less than or equal to the pivot will be colored blue; items

greater than the pivot will be colored light purple

CMSC 132 Summer 2017 96

Page 97: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Search for the first value at the left end of the array that is greater than the pivot value

44 75 23 43 55 12 64 77 33

CMSC 132 Summer 2017 97

Page 98: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Search for the first value at the left end of the array that is greater than the pivot value

up

44 75 23 43 55 12 64 77 33

CMSC 132 Summer 2017 98

Page 99: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Then search for the first value at the right end of the array that is less than or equal to the pivot

value

up

44 75 23 43 55 12 64 77 33

CMSC 132 Summer 2017 99

Page 100: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Then search for the first value at the right end of the array that is less than or equal to the pivot

value

up down

44 75 23 43 55 12 64 77 33

CMSC 132 Summer 2017 100

Page 101: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Exchange these values

up down

44 75 23 43 55 12 64 77 33

CMSC 132 Summer 2017 101

Page 102: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Exchange these values

44 33 23 43 55 12 64 77 75

CMSC 132 Summer 2017 102

Page 103: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Repeat

44 33 23 43 55 12 64 77 75

CMSC 132 Summer 2017 103

Page 104: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Find first value at left end greater than pivot

up

44 33 23 43 55 12 64 77 75

CMSC 132 Summer 2017 104

Page 105: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Find first value at right end less than or equal to pivot

up down

44 33 23 43 55 12 64 77 75

CMSC 132 Summer 2017 105

Page 106: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Exchange

44 33 23 43 12 55 64 77 75

CMSC 132 Summer 2017 106

Page 107: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Repeat

44 33 23 43 12 55 64 77 75

CMSC 132 Summer 2017 107

Page 108: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Find first element at left end greater than pivot

up

44 33 23 43 12 55 64 77 75

CMSC 132 Summer 2017 108

Page 109: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Find first element at right end less than or equal to pivot

updown

44 33 23 43 12 55 64 77 75

CMSC 132 Summer 2017 109

Page 110: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Since down has "passed" up, do not exchange

updown

44 33 23 43 12 55 64 77 75

CMSC 132 Summer 2017 110

Page 111: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Exchange the pivot value with the value at down

updown

44 33 23 43 12 55 64 77 75

CMSC 132 Summer 2017 111

Page 112: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

Exchange the pivot value with the value at down

down

12 33 23 43 44 55 64 77 75

CMSC 132 Summer 2017 112

Page 113: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Partitioning (cont.)

The pivot value is in the correct position; return the value of

down and assign it to the pivot index pivIndex

down

12 33 23 43 44 55 64 77 75

CMSC 132 Summer 2017 113

Page 114: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Code for partition when Pivot is the largest or smallest value

CMSC 132 Summer 2017 114

Page 115: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Revised Partition Algorithm

• Quicksort is O(n2) when each split yields one empty subarray, which is the case when the array is presorted

• A better solution is to pick the pivot value in a way that is less likely to lead to a bad split• Use three references: first, middle, last• Select the median of the these items as the pivot

CMSC 132 Summer 2017 115

Page 116: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Revised Partitioning

44 75 23 43 55 12 64 77 33

CMSC 132 Summer 2017 116

Page 117: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Revised Partitioning (cont.)

44 75 23 43 55 12 64 77 33

middlefirst last

CMSC 132 Summer 2017 117

Page 118: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Revised Partitioning (cont.)

44 75 23 43 55 12 64 77 33

Sort these values

middlefirst last

CMSC 132 Summer 2017 118

Page 119: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Revised Partitioning (cont.)

33 75 23 43 44 12 64 77 55

Sort these values

middlefirst last

CMSC 132 Summer 2017 119

Page 120: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Revised Partitioning (cont.)

33 75 23 43 44 12 64 77 55

Exchange middle with first

middlefirst last

CMSC 132 Summer 2017 120

Page 121: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Revised Partitioning (cont.)

44 75 23 43 33 12 64 77 55

Exchange middle with first

middlefirst last

CMSC 132 Summer 2017 121

Page 122: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Trace of Revised Partitioning (cont.)

44 75 23 43 33 12 64 77 55

Run the partition algorithm using the first element as the

pivot

CMSC 132 Summer 2017 122

Page 123: CMSC 132: Object-Oriented Programming II · CMSC 132: Object-Oriented Programming II ... CMSC 132 Summer 2017 56. Trace of Heapsort (cont.) 74 37 66 26 32 39 29 20 6 18 28 76 89 CMSC

Sorting Algorithm Comparison

Name Best Average

Worst Memory Stable

Bubble Sort n n2 n2 1 yesSelection Sort

n2 n2 n2 1 no

Insertion Sort

n n2 n2 1 yes

Merge Sort nlogn nlogn nlogn n yesQuick Sort nlogn nlogn n2 log n noHeap Sort nlogn nlogn nlogn 1 no

CMSC 132 Summer 2017 123