Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200...

34
Data Structure & Algorithms in JAVA 5 th edition Michael T. Goodrich Roberto Tamassia Chapter 11: Sorting, Sets, and Selection CPSC 3200 Algorithm Analysis and Advanced Data Structure

Transcript of Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200...

Page 1: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

Data Structure & Algorithms in JAVA

5th editionMichael T. GoodrichRoberto Tamassia

Chapter 11: Sorting, Sets, and Selection

CPSC 3200Algorithm Analysis and Advanced Data Structure

Page 2: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

Chapter Topics• Insertion Sort.• Selection Sort.• Bubble Sort.• Heap Sort.• Merge-sort.• Quick-sort.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 3: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

3

Insertion SortAlgorithm InsertionSort(A):

Input: An array A of n comparable elementsOutput: The array A with elements rearranged in nondecreasing orderfor i ← 1 to n−1 do

{Insert A[i] at its proper location in A[0],A[1],...,A[i−1]}cur ← A[i]j ← i−1while j ≥ 0 and a[j] > cur do

A[j+1] ← A[j]j ← j−1

A[j+1] ← cur {cur is now in the right place}

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 4: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

4

Selection SortAlgorithm SelectionSort(A)

Input: An array A of n comparable elementsOutput:The array A with elements rearranged in nondecreasing order

n := length[A] for i 1 to n do

j FindIndexOfSmallest( A, i, n ) swap A[i] with A[j]

retrun A

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Algorithm FindIndexOfSmallest( A, i, n ) smallestAt i for j (i+1) to n do

if ( A[j] < A[smallestAt] ) smallestAt j

return smallestAt

© 2010 Goodrich, Tamassia

Page 5: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

5

Bubble Sort

Algorithm BubbleSort(A)Input: An array A of n comparable elementsOutput: The array A with elements rearranged in nondecreasing orderfor i ← 0 to N - 2 do

for J ← 0 to N - 2 do if (A( J ) > A( J + 1 ) then

temp ← A( J ) A( J ) ← A( J + 1 ) A( J + 1 ) ← temp

return A

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 6: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

6

Divide-and-Conquer

• Divide-and conquer is a general algorithm design paradigm:• Divide: divide the input data S in

two or more disjoint subsets S1, S2, …• Recur: solve the subproblems

recursively• Conquer: combine the solutions for

S1, S2, …, into a solution for S

• The base case for the recursion are subproblems of constant size.

• Analysis can be done using recurrence equations.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 7: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

7

Divide-and-Conquer• Divide-and conquer is a

general algorithm design paradigm:• Divide: divide the input data

S in two disjoint subsets S1 and S2

• Recur: solve the subproblems associated with S1 and S2

• Conquer: combine the solutions for S1 and S2 into a solution for S

• The base case for the recursion are subproblems of size 0 or 1.

• Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm.

• Like heap-sort• It uses a comparator.• It has O(n log n) running

time.• Unlike heap-sort• It does not use an auxiliary

priority queue• It accesses data in a

sequential manner (suitable to sort data on a disk)

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 8: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

8

Merge-Sort

• Merge-sort on an input sequence S with n elements consists of three steps:• Divide: partition S into two

sequences S1 and S2 of about n/2 elements each• Recur: recursively sort S1

and S2

• Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input: sequence S with n

elements, comparator C Output: sequence S sorted

according to Cif S.size() > 1

(S1, S2) partition(S, n/2)

mergeSort(S1, C)

mergeSort(S2, C)

S merge(S1, S2)

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 9: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

9

Merging Two Sorted Sequences• The conquer step of

merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time.

Algorithm merge(A, B)Input: sequences A and B with

n/2 elements each

Output: sorted sequence of A B

S empty sequence

while A.isEmpty() B.isEmpty()if A.first().element() <

B.first().element()

S.addLast(A.remove(A.first()))else

S.addLast(B.remove(B.first()))while A.isEmpty()S.addLast(A.remove(A.first()))while B.isEmpty()S.addLast(B.remove(B.first()))return S

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 10: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

10

Merge-Sort Tree

• An execution of merge-sort is depicted by a binary tree• each node represents a recursive call of merge-sort and stores• unsorted sequence before the execution and its partition• sorted sequence at the end of the execution

• the root is the initial call • the leaves are calls on subsequences of size 0 or 1

7 2 9 4 2 4 7 9

7 2 2 7 9 4 4 9

7 7 2 2 9 9 4 4

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 11: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

11

Execution Example

• Partition

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 12: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

12

Execution Example (cont.)

• Recursive call, partition

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 13: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

13

Execution Example (cont.)

• Recursive call, partition

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 14: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

14

Execution Example (cont.)

• Recursive call, base case

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 15: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

15

Execution Example (cont.)

• Recursive call, base case

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 16: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

16

Execution Example (cont.)

• Merge

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 17: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

17

Execution Example (cont.)

• Recursive call, …, base case, merge

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

9 9 4 4

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 18: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

18

Execution Example (cont.)

• Merge

7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 19: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

19

Execution Example (cont.)

• Recursive call, …, merge, merge

7 2 9 4 2 4 7 9 3 8 6 1 1 3 6 8

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 20: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

20

Execution Example (cont.)

• Merge

7 2 9 4 2 4 7 9 3 8 6 1 1 3 6 8

7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6

7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 21: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

21

Quick-Sort

• Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:• Divide: pick a random element

x (called pivot) and partition S into • L elements less than x• E elements equal x• G elements greater than x

• Recur: sort L and G• Conquer: join L, E and G

x

x

L GE

x

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 22: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

22

Partition

• We partition an input sequence as follows:• We remove, in turn, each element y

from S and • We insert y into L, E or G,

depending on the result of the comparison with the pivot x

• Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time

• Thus, the partition step of quick-sort takes O(n) time.

Algorithm partition(S, p)Input: sequence S, position p of

pivot Output: subsequences L, E, G

of the elements of S less

than, equal to,or greater than the

pivot, resp.L, E, G empty sequences

x S.remove(p) while S.isEmpty()

y S.remove(S.first())if y < x

L.addLast(y)else if y = x

E.addLast(y)else { y > x }

G.addLast(y)return L, E, G

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 23: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

23

Quick-Sort Tree

• An execution of quick-sort is depicted by a binary tree• Each node represents a recursive call of quick-sort and stores• Unsorted sequence before the execution and its pivot• Sorted sequence at the end of the execution

• The root is the initial call • The leaves are calls on subsequences of size 0 or 1

7 4 9 6 2 2 4 6 7 9

4 2 2 4 7 9 7 9

2 2 9 9CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 24: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

24

Execution Example

• Pivot selection

7 2 9 4 2 4 7 9

2 2

7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9

3 8 6 1 1 3 8 6

3 3 8 89 4 4 9

9 9 4 4

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 25: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

25

Execution Example (cont.)

• Partition, recursive call, pivot selection

2 4 3 1 2 4 7 9

9 4 4 9

9 9 4 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9

3 8 6 1 1 3 8 6

3 3 8 82 2

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 26: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

26

Execution Example (cont.)

• Partition, recursive call, base case

2 4 3 1 2 4 7

1 1 9 4 4 9

9 9 4 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9

3 8 6 1 1 3 8 6

3 3 8 8

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 27: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

27

Execution Example (cont.)

• Recursive call, …, base case, join

3 8 6 1 1 3 8 6

3 3 8 8

7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9

2 4 3 1 1 2 3 4

1 1 4 3 3 4

9 9 4 4

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 28: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

28

Execution Example (cont.)

• Recursive call, pivot selection

7 9 7 1 1 3 8 6

8 8

7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9

2 4 3 1 1 2 3 4

1 1 4 3 3 4

9 9 4 4

9 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 29: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

29

Execution Example (cont.)

• Partition, …, recursive call, base case

7 9 7 1 1 3 8 6

8 8

7 2 9 4 3 7 6 1 1 2 3 4 6 7 8 9

2 4 3 1 1 2 3 4

1 1 4 3 3 4

9 9 4 4

9 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 30: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

30

Execution Example (cont.)

• Join, join

7 9 7 17 7 9

8 8

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

2 4 3 1 1 2 3 4

1 1 4 3 3 4

9 9 4 4

9 9

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 31: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

31

In-Place Quick-Sort

• Quick-sort can be implemented to run in-place

• In the partition step, we use replace operations to rearrange the elements of the input sequence such that• the elements less than the pivot

have rank less than h• the elements equal to the pivot

have rank between h and k• the elements greater than the

pivot have rank greater than k• The recursive calls consider

• elements with rank less than h• elements with rank greater than k

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 32: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

32CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Algorithm inPlaceQuickSort(S,a,b):Input: An array S of distinct elements; integers a and bOutput: Array S with elements originally from indices from a to b, inclusive,sorted in nondecreasing order from indices a to bif a ≥ b then return {at most one element in subrange}p ← S[b] {the pivot}l ← a {will scan rightward}r ← b−1 {will scan leftward}while l ≤ r do {find an element larger than the pivot}

while l ≤ r and S[l] ≤ p dol ← l+1

{find an element smaller than the pivot}while r ≥ l and S[r] ≥ p do

r ← r−1if l < r then

swap the elements at S[l] and S[r]{put the pivot into its final place}swap the elements at S[l] and S[b]{recursive calls}inPlaceQuickSort(S,a,l −1)inPlaceQuickSort(S,l +1,b){we are done at this point, since the sorted subarrays are already consecutive}

© 2010 Goodrich, Tamassia

Page 33: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

33

Summary of Sorting AlgorithmsAlgorithm Time Notes

selection-sort O(n2) in-place slow (good for small inputs)

insertion-sort O(n2) in-place slow (good for small inputs)

bubble-sort O(n2) in-place slow (good for small inputs)

quick-sort O(n log n)expected

in-place, randomized fastest (good for large inputs)

heap-sort O(n log n) in-place fast (good for large inputs)

merge-sort O(n log n) sequential data access fast (good for huge inputs)

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 34: Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

34

End of Chapter 11

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013