more on sorting: quick sort and heap sort - inf.usi.ch filemore on sorting: quick sort and heap sort...

161
More on Sorting: Quick Sort and Heap Sort Antonio Carzaniga Faculty of Informatics University of Lugano March 5, 2013 © 2006 Antonio Carzaniga

Upload: doanhuong

Post on 27-May-2019

268 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

More on Sorting:

Quick Sort and Heap Sort

Antonio Carzaniga

Faculty of InformaticsUniversity of Lugano

March 5, 2013

© 2006 Antonio Carzaniga

Page 2: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Outline

Another divide-and-conquer sorting algorithm

The heap

Heap sort

© 2006 Antonio Carzaniga

Page 3: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

© 2006 Antonio Carzaniga

Page 4: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

© 2006 Antonio Carzaniga

Page 5: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort

© 2006 Antonio Carzaniga

Page 6: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

© 2006 Antonio Carzaniga

Page 7: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort

© 2006 Antonio Carzaniga

Page 8: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

© 2006 Antonio Carzaniga

Page 9: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort

© 2006 Antonio Carzaniga

Page 10: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

© 2006 Antonio Carzaniga

Page 11: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort

© 2006 Antonio Carzaniga

Page 12: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

© 2006 Antonio Carzaniga

Page 13: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

?? Θ(n log n) yes

?? Θ(n log n) yes

© 2006 Antonio Carzaniga

Page 14: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

© 2006 Antonio Carzaniga

Page 15: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

E.g., A = 〈2,36,5,21,8,13,11,20,5,4,1〉

© 2006 Antonio Carzaniga

Page 16: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

E.g., A = 〈2,36,5,21,8,13,11,20,5,4,1〉

we pick a splitting value, say v = 5

© 2006 Antonio Carzaniga

Page 17: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

E.g., A = 〈2,36,5,21,8,13,11,20,5,4,1〉

we pick a splitting value, say v = 5

AL = 〈2,4,1〉

© 2006 Antonio Carzaniga

Page 18: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

E.g., A = 〈2,36,5,21,8,13,11,20,5,4,1〉

we pick a splitting value, say v = 5

AL = 〈2,4,1〉 Av = 〈5,5〉

© 2006 Antonio Carzaniga

Page 19: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

E.g., A = 〈2,36,5,21,8,13,11,20,5,4,1〉

we pick a splitting value, say v = 5

AL = 〈2,4,1〉 Av = 〈5,5〉 AR = 〈36,21,8,13,11,20〉

© 2006 Antonio Carzaniga

Page 20: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

E.g., A = 〈2,36,5,21,8,13,11,20,5,4,1〉

we pick a splitting value, say v = 5

AL = 〈2,4,1〉 Av = 〈5,5〉 AR = 〈36,21,8,13,11,20〉

Can we use the same idea for sorting A?

© 2006 Antonio Carzaniga

Page 21: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Using the Partitioning Algorithm

Basic step: partition A in three parts based on a chosen value

v ∈ A

◮ AL contains the set of elements that are less than v

◮ Av contains the set of elements that are equal to v

◮ AR contains the set of elements that are greater then v

E.g., A = 〈2,36,5,21,8,13,11,20,5,4,1〉

we pick a splitting value, say v = 5

AL = 〈2,4,1〉 Av = 〈5,5〉 AR = 〈36,21,8,13,11,20〉

Can we use the same idea for sorting A?

Can we partition A in place?

© 2006 Antonio Carzaniga

Page 22: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

© 2006 Antonio Carzaniga

Page 23: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

© 2006 Antonio Carzaniga

Page 24: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1

© 2006 Antonio Carzaniga

Page 25: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

© 2006 Antonio Carzaniga

Page 26: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

© 2006 Antonio Carzaniga

Page 27: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

2 4 1 5 5

© 2006 Antonio Carzaniga

Page 28: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

2 4 1 5 5 8

© 2006 Antonio Carzaniga

Page 29: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

2 4 1 5 5 8 11 20 13 36 21

© 2006 Antonio Carzaniga

Page 30: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

2 4 1 5 5 8 11 20 13 36 21

q = 6

© 2006 Antonio Carzaniga

Page 31: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

2 4 1 5 5 8 11 20 13 36 21

q = 6

A[1 . . .q− 1]

© 2006 Antonio Carzaniga

Page 32: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Strategy for Sorting

Problem: sorting

Idea: rearrange the sequence A[1 . . .n] in three parts based on

a chosen “pivot” value v ∈ A

◮ A[1 . . .q− 1] contain elements that are less than or equal to v

◮ A[q] = v

◮ A[q+ 1 . . .n] contain elements that are greater than v

2 36 5 21 8 13 11 20 5 4 1 v = 8

2 4 1 5 5 8 11 20 13 36 21

q = 6

A[1 . . .q− 1] A[q+ 1 . . .n]

© 2006 Antonio Carzaniga

Page 33: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Divide-and-Conquer for Sorting

Divide:

© 2006 Antonio Carzaniga

Page 34: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Divide-and-Conquer for Sorting

Divide: partition A in A[1 . . .q− 1] and A[q+ 1 . . .n] such that

1 ≤ i < q < j ≤ n ⇒ A[i] ≤ A[q] ≤ A[j]

© 2006 Antonio Carzaniga

Page 35: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Divide-and-Conquer for Sorting

Divide: partition A in A[1 . . .q− 1] and A[q+ 1 . . .n] such that

1 ≤ i < q < j ≤ n ⇒ A[i] ≤ A[q] ≤ A[j]

Conquer:

© 2006 Antonio Carzaniga

Page 36: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Divide-and-Conquer for Sorting

Divide: partition A in A[1 . . .q− 1] and A[q+ 1 . . .n] such that

1 ≤ i < q < j ≤ n ⇒ A[i] ≤ A[q] ≤ A[j]

Conquer: sort A[1 . . .q− 1] and A[q+ 1 . . .n]

© 2006 Antonio Carzaniga

Page 37: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Divide-and-Conquer for Sorting

Divide: partition A in A[1 . . .q− 1] and A[q+ 1 . . .n] such that

1 ≤ i < q < j ≤ n ⇒ A[i] ≤ A[q] ≤ A[j]

Conquer: sort A[1 . . .q− 1] and A[q+ 1 . . .n]

Combine:

© 2006 Antonio Carzaniga

Page 38: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Divide-and-Conquer for Sorting

Divide: partition A in A[1 . . .q− 1] and A[q+ 1 . . .n] such that

1 ≤ i < q < j ≤ n ⇒ A[i] ≤ A[q] ≤ A[j]

Conquer: sort A[1 . . .q− 1] and A[q+ 1 . . .n]

Combine: nothing to do here

◮ notice the difference with MergeSort

© 2006 Antonio Carzaniga

Page 39: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Another Divide-and-Conquer for Sorting

Divide: partition A in A[1 . . .q− 1] and A[q+ 1 . . .n] such that

1 ≤ i < q < j ≤ n ⇒ A[i] ≤ A[q] ≤ A[j]

Conquer: sort A[1 . . .q− 1] and A[q+ 1 . . .n]

Combine: nothing to do here

◮ notice the difference with MergeSort

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

© 2006 Antonio Carzaniga

Page 40: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

© 2006 Antonio Carzaniga

Page 41: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

© 2006 Antonio Carzaniga

Page 42: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

© 2006 Antonio Carzaniga

Page 43: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

36 11 5 21 1 13 2 20 5 4 8

© 2006 Antonio Carzaniga

Page 44: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

36 11 5 21 1 13 2 20 5 4 8 v = A[end]

© 2006 Antonio Carzaniga

Page 45: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

36 11 5 21 1 13 2 20 5 4 8 v = A[end]

q

i

© 2006 Antonio Carzaniga

Page 46: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

36 11 5 21 1 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 47: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

36 11 5 21 1 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 48: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

36 11 5 21 1 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 49: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 11 36 21 1 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 50: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 11 36 21 1 13 2 20 5 4 8

i

q

© 2006 Antonio Carzaniga

Page 51: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 11 36 21 1 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 52: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 11 36 21 1 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 53: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 11 36 21 1 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 54: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 36 21 11 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 55: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 36 21 11 13 2 20 5 4 8

i

q

© 2006 Antonio Carzaniga

Page 56: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 36 21 11 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 57: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 36 21 11 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 58: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 36 21 11 13 2 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 59: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 21 11 13 36 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 60: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 21 11 13 36 20 5 4 8

i

q

© 2006 Antonio Carzaniga

Page 61: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 21 11 13 36 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 62: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 21 11 13 36 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 63: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 21 11 13 36 20 5 4 8

q

i

© 2006 Antonio Carzaniga

Page 64: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 11 13 36 20 21 4 8

q

i

© 2006 Antonio Carzaniga

Page 65: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 11 13 36 20 21 4 8

i

q

© 2006 Antonio Carzaniga

Page 66: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 11 13 36 20 21 4 8

q

i

© 2006 Antonio Carzaniga

Page 67: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 11 13 36 20 21 4 8

q

i

© 2006 Antonio Carzaniga

Page 68: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 4 13 36 20 21 11 8

q

i

© 2006 Antonio Carzaniga

Page 69: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 4 13 36 20 21 11 8

i

q

© 2006 Antonio Carzaniga

Page 70: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 4 13 36 20 21 11 8

q

i

© 2006 Antonio Carzaniga

Page 71: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 4 8 36 20 21 11 13

q

i

© 2006 Antonio Carzaniga

Page 72: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Partition

Start with q = 1◮ i.e., assume all elements are greater than the pivot

Scan the array left-to-right, starting at position 2

If an element A[i] is less than or equal to pivot, then swap it

with the current q position and shift q to the right

Loop invariant

◮ begin ≤ k < q ⇒ A[k] ≤ v

◮ q < k < i ⇒ A[k] > v

5 1 2 5 4 8 36 20 21 11 13

q

© 2006 Antonio Carzaniga

Page 73: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complete QuickSort Algorithm

Partition(A,begin, end)

1 q = begin

2 v = A[end]

3 for i = begin to end

4 if A[i] ≤ v

5 swap A[i] and A[q]

6 q = q+ 1

7 return q− 1

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

© 2006 Antonio Carzaniga

Page 74: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of Partition

Partition(A,begin, end)

1 q = begin

2 v = A[end]

3 for i = begin to end

4 if A[i] ≤ v

5 swap A[i] and A[q]

6 q = q+ 1

7 return q− 1

© 2006 Antonio Carzaniga

Page 75: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of Partition

Partition(A,begin, end)

1 q = begin

2 v = A[end]

3 for i = begin to end

4 if A[i] ≤ v

5 swap A[i] and A[q]

6 q = q+ 1

7 return q− 1

T (n) = Θ(n)

© 2006 Antonio Carzaniga

Page 76: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

© 2006 Antonio Carzaniga

Page 77: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Worst case

© 2006 Antonio Carzaniga

Page 78: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Worst case

◮ q = begin or q = end

© 2006 Antonio Carzaniga

Page 79: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Worst case

◮ q = begin or q = end

◮ the partition transforms P of size n in P of size n− 1

© 2006 Antonio Carzaniga

Page 80: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Worst case

◮ q = begin or q = end

◮ the partition transforms P of size n in P of size n− 1

T (n) = T (n− 1)+Θ(n)

© 2006 Antonio Carzaniga

Page 81: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Worst case

◮ q = begin or q = end

◮ the partition transforms P of size n in P of size n− 1

T (n) = T (n− 1)+Θ(n)

T (n) = Θ(n2)

© 2006 Antonio Carzaniga

Page 82: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort (2)

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

© 2006 Antonio Carzaniga

Page 83: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort (2)

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Best case

© 2006 Antonio Carzaniga

Page 84: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort (2)

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Best case

◮ q = ⌈n/2⌉

© 2006 Antonio Carzaniga

Page 85: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort (2)

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Best case

◮ q = ⌈n/2⌉

◮ the partition transforms P of size n into two problems P of size

⌊n/2⌋ and ⌈n/2⌉ − 1, respectively

© 2006 Antonio Carzaniga

Page 86: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort (2)

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Best case

◮ q = ⌈n/2⌉

◮ the partition transforms P of size n into two problems P of size

⌊n/2⌋ and ⌈n/2⌉ − 1, respectively

T (n) = 2T (n/2) +Θ(n)

© 2006 Antonio Carzaniga

Page 87: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Complexity of QuickSort (2)

QuickSort(A,begin, end)

1 if begin < end

2 q = Partition(A,begin, end)

3 QuickSort(A,begin,q− 1)

4 QuickSort(A,q+ 1, end)

Best case

◮ q = ⌈n/2⌉

◮ the partition transforms P of size n into two problems P of size

⌊n/2⌋ and ⌈n/2⌉ − 1, respectively

T (n) = 2T (n/2) +Θ(n)

T (n) = Θ(n log n)

© 2006 Antonio Carzaniga

Page 88: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

© 2006 Antonio Carzaniga

Page 89: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

© 2006 Antonio Carzaniga

Page 90: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

QuickSort

© 2006 Antonio Carzaniga

Page 91: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

QuickSort Θ(n2) Θ(n log n) Θ(n log n) yes

© 2006 Antonio Carzaniga

Page 92: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

QuickSort Θ(n2) Θ(n log n) Θ(n log n) yes

?? Θ(n log n) yes

© 2006 Antonio Carzaniga

Page 93: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

© 2006 Antonio Carzaniga

Page 94: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

© 2006 Antonio Carzaniga

Page 95: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

© 2006 Antonio Carzaniga

Page 96: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

◮ Build-Max-Heap(A) rearranges A into a max-heap

◮ Heap-Insert(H,key) inserts key in the heap

◮ Heap-Extract-Max(H) extracts the maximum key

◮ H.heap-size is the number of keys in H

© 2006 Antonio Carzaniga

Page 97: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

◮ Build-Max-Heap(A) rearranges A into a max-heap

◮ Heap-Insert(H,key) inserts key in the heap

◮ Heap-Extract-Max(H) extracts the maximum key

◮ H.heap-size is the number of keys in H

Two kinds of binary heaps

© 2006 Antonio Carzaniga

Page 98: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

◮ Build-Max-Heap(A) rearranges A into a max-heap

◮ Heap-Insert(H,key) inserts key in the heap

◮ Heap-Extract-Max(H) extracts the maximum key

◮ H.heap-size is the number of keys in H

Two kinds of binary heaps

◮ max-heaps

© 2006 Antonio Carzaniga

Page 99: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

◮ Build-Max-Heap(A) rearranges A into a max-heap

◮ Heap-Insert(H,key) inserts key in the heap

◮ Heap-Extract-Max(H) extracts the maximum key

◮ H.heap-size is the number of keys in H

Two kinds of binary heaps

◮ max-heaps

◮ min-heaps

© 2006 Antonio Carzaniga

Page 100: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

◮ Build-Max-Heap(A) rearranges A into a max-heap

◮ Heap-Insert(H,key) inserts key in the heap

◮ Heap-Extract-Max(H) extracts the maximum key

◮ H.heap-size is the number of keys in H

Two kinds of binary heaps

◮ max-heaps

◮ min-heaps

Useful applications

© 2006 Antonio Carzaniga

Page 101: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

◮ Build-Max-Heap(A) rearranges A into a max-heap

◮ Heap-Insert(H,key) inserts key in the heap

◮ Heap-Extract-Max(H) extracts the maximum key

◮ H.heap-size is the number of keys in H

Two kinds of binary heaps

◮ max-heaps

◮ min-heaps

Useful applications

◮ sorting

© 2006 Antonio Carzaniga

Page 102: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap

Our first real data structure

Interface

◮ Build-Max-Heap(A) rearranges A into a max-heap

◮ Heap-Insert(H,key) inserts key in the heap

◮ Heap-Extract-Max(H) extracts the maximum key

◮ H.heap-size is the number of keys in H

Two kinds of binary heaps

◮ max-heaps

◮ min-heaps

Useful applications

◮ sorting

◮ priority queue

© 2006 Antonio Carzaniga

Page 103: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Structure

© 2006 Antonio Carzaniga

Page 104: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Structure

Conceptually a full binary tree

© 2006 Antonio Carzaniga

Page 105: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Structure

Conceptually a full binary tree

© 2006 Antonio Carzaniga

Page 106: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Structure

Conceptually a full binary tree

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

© 2006 Antonio Carzaniga

Page 107: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Structure

Conceptually a full binary tree

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Implemented as an array

© 2006 Antonio Carzaniga

Page 108: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Structure

Conceptually a full binary tree

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Implemented as an array

© 2006 Antonio Carzaniga

Page 109: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Structure

Conceptually a full binary tree

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Implemented as an array

1 2 3 4 5 6 7 8 9 101112131415

© 2006 Antonio Carzaniga

Page 110: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

© 2006 Antonio Carzaniga

Page 111: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

© 2006 Antonio Carzaniga

Page 112: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

© 2006 Antonio Carzaniga

Page 113: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

© 2006 Antonio Carzaniga

Page 114: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Parent(i)

return ⌊i/2⌋

Left(i)

return 2i

Right(i)

return 2i + 1

© 2006 Antonio Carzaniga

Page 115: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Parent(i)

return ⌊i/2⌋

Left(i)

return 2i

Right(i)

return 2i + 1

1 2 3 4 5 6 7 8 9 101112131415

© 2006 Antonio Carzaniga

Page 116: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Parent(i)

return ⌊i/2⌋

Left(i)

return 2i

Right(i)

return 2i + 1

1 2 3 4 5 6 7 8 9 101112131415

© 2006 Antonio Carzaniga

Page 117: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Parent(i)

return ⌊i/2⌋

Left(i)

return 2i

Right(i)

return 2i + 1

1 2 3 4 5 6 7 8 9 101112131415

Max-heap property: for all i > 1, A[Parent(i)] ≥ A[i]

© 2006 Antonio Carzaniga

Page 118: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Example

Max-heap property: for all i > 1, A[Parent(i)] ≥ A[i]

© 2006 Antonio Carzaniga

Page 119: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Example

Max-heap property: for all i > 1, A[Parent(i)] ≥ A[i]

E.g.,

© 2006 Antonio Carzaniga

Page 120: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Example

Max-heap property: for all i > 1, A[Parent(i)] ≥ A[i]

E.g.,

36

21 11

13 20 8 5

13 2 4 15 8

© 2006 Antonio Carzaniga

Page 121: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Example

Max-heap property: for all i > 1, A[Parent(i)] ≥ A[i]

E.g.,

36

21 11

13 20 8 5

13 2 4 15 8

Where is the max element?

© 2006 Antonio Carzaniga

Page 122: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Example

Max-heap property: for all i > 1, A[Parent(i)] ≥ A[i]

E.g.,

36

21 11

13 20 8 5

13 2 4 15 8

Where is the max element?

How can we implement Heap-Extract-Max?

© 2006 Antonio Carzaniga

Page 123: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap-Extract-Max

Heap-Extract-Max procedure

◮ extract the max key

◮ rearrange the heap to maintain the max-heap property

© 2006 Antonio Carzaniga

Page 124: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap-Extract-Max

Heap-Extract-Max procedure

◮ extract the max key

◮ rearrange the heap to maintain the max-heap property

36

21 11

13 20 8 5

13 2 4 15 8

© 2006 Antonio Carzaniga

Page 125: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap-Extract-Max

Heap-Extract-Max procedure

◮ extract the max key

◮ rearrange the heap to maintain the max-heap property

21 11

13 20 8 5

13 2 4 15 8

© 2006 Antonio Carzaniga

Page 126: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap-Extract-Max

Heap-Extract-Max procedure

◮ extract the max key

◮ rearrange the heap to maintain the max-heap property

8

21 11

13 20 8 5

13 2 4 15

© 2006 Antonio Carzaniga

Page 127: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap-Extract-Max

Heap-Extract-Max procedure

◮ extract the max key

◮ rearrange the heap to maintain the max-heap property

8

21 11

13 20 8 5

13 2 4 15

Now we have two subtrees in which the max-heap property

holds

© 2006 Antonio Carzaniga

Page 128: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i) procedure

◮ assume: the left subtree of node i is a heap

◮ goal: rearrange the heap to maintain the max-heap property

© 2006 Antonio Carzaniga

Page 129: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i) procedure

◮ assume: the left subtree of node i is a heap

◮ goal: rearrange the heap to maintain the max-heap property

8

21 11

13 20 8 5

13 2 4 15

© 2006 Antonio Carzaniga

Page 130: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i) procedure

◮ assume: the left subtree of node i is a heap

◮ goal: rearrange the heap to maintain the max-heap property

21

8 11

13 20 8 5

13 2 4 15

© 2006 Antonio Carzaniga

Page 131: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i) procedure

◮ assume: the left subtree of node i is a heap

◮ goal: rearrange the heap to maintain the max-heap property

21

20 11

13 8 8 5

13 2 4 15

© 2006 Antonio Carzaniga

Page 132: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i) procedure

◮ assume: the left subtree of node i is a heap

◮ goal: rearrange the heap to maintain the max-heap property

21

20 11

13 15 8 5

13 2 4 8

© 2006 Antonio Carzaniga

Page 133: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i) procedure

◮ assume: the left subtree of node i is a heap

◮ goal: rearrange the heap to maintain the max-heap property

21

20 11

13 15 8 5

13 2 4 8

© 2006 Antonio Carzaniga

Page 134: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i)

1 l = Left(i)

2 r = Right(i)

3 if l ≤ A.heap-size and A[l] > A[i]

4 largest = l

5 else largest = i

6 if r ≤ A.heap-size and A[r] > A[largest]

7 largest = r

8 if largest 6= i

9 swap A[i] and A[largest]

10 Max-Heapify(A, largest)

© 2006 Antonio Carzaniga

Page 135: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i)

1 l = Left(i)

2 r = Right(i)

3 if l ≤ A.heap-size and A[l] > A[i]

4 largest = l

5 else largest = i

6 if r ≤ A.heap-size and A[r] > A[largest]

7 largest = r

8 if largest 6= i

9 swap A[i] and A[largest]

10 Max-Heapify(A, largest)

Complexity of Max-Heapify?

© 2006 Antonio Carzaniga

Page 136: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i)

1 l = Left(i)

2 r = Right(i)

3 if l ≤ A.heap-size and A[l] > A[i]

4 largest = l

5 else largest = i

6 if r ≤ A.heap-size and A[r] > A[largest]

7 largest = r

8 if largest 6= i

9 swap A[i] and A[largest]

10 Max-Heapify(A, largest)

Complexity of Max-Heapify? The height of the tree!

© 2006 Antonio Carzaniga

Page 137: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Max-Heapify

Max-Heapify(A, i)

1 l = Left(i)

2 r = Right(i)

3 if l ≤ A.heap-size and A[l] > A[i]

4 largest = l

5 else largest = i

6 if r ≤ A.heap-size and A[r] > A[largest]

7 largest = r

8 if largest 6= i

9 swap A[i] and A[largest]

10 Max-Heapify(A, largest)

Complexity of Max-Heapify? The height of the tree!

T (n) = Θ(log n)

© 2006 Antonio Carzaniga

Page 138: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

© 2006 Antonio Carzaniga

Page 139: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

© 2006 Antonio Carzaniga

Page 140: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

© 2006 Antonio Carzaniga

Page 141: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

© 2006 Antonio Carzaniga

Page 142: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆

© 2006 Antonio Carzaniga

Page 143: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆ • •

• • •

© 2006 Antonio Carzaniga

Page 144: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆ • •

• • •

⋆ heapify points

• leaves

© 2006 Antonio Carzaniga

Page 145: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆ • •

• • •

⋆ heapify points

• leaves

1 2 3 4 5 6 7 8 9 10© 2006 Antonio Carzaniga

Page 146: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Building a Heap

Build-Max-Heap(A)

1 A.heap-size = length(A)

2 for i = ⌊length(A)/2⌋ downto 1

3 Max-Heapify(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆ • •

• • •

⋆ heapify points

• leaves

1 2 3 4 5 6 7 8 9 10

⋆ ⋆ ⋆ ⋆ ⋆ • • • • •

© 2006 Antonio Carzaniga

Page 147: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap Sort

Idea: we can use a heap to sort an array

© 2006 Antonio Carzaniga

Page 148: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap Sort

Idea: we can use a heap to sort an array

Heap-Sort(A)

1 Build-Max-Heap(A)

2 for i = length(A) downto 1

3 swap A[i] and A[1]

4 A.heap-size = A.heap-size− 1

5 Max-Heapify(A,1)

© 2006 Antonio Carzaniga

Page 149: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap Sort

Idea: we can use a heap to sort an array

Heap-Sort(A)

1 Build-Max-Heap(A)

2 for i = length(A) downto 1

3 swap A[i] and A[1]

4 A.heap-size = A.heap-size− 1

5 Max-Heapify(A,1)

What is the complexity of Heap-Sort?

© 2006 Antonio Carzaniga

Page 150: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap Sort

Idea: we can use a heap to sort an array

Heap-Sort(A)

1 Build-Max-Heap(A)

2 for i = length(A) downto 1

3 swap A[i] and A[1]

4 A.heap-size = A.heap-size− 1

5 Max-Heapify(A,1)

What is the complexity of Heap-Sort?

T (n) = Θ(n log n)

© 2006 Antonio Carzaniga

Page 151: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Heap Sort

Idea: we can use a heap to sort an array

Heap-Sort(A)

1 Build-Max-Heap(A)

2 for i = length(A) downto 1

3 swap A[i] and A[1]

4 A.heap-size = A.heap-size− 1

5 Max-Heapify(A,1)

What is the complexity of Heap-Sort?

T (n) = Θ(n log n)

Benefits

◮ in-place sorting; worst-case is Θ(n log n)

© 2006 Antonio Carzaniga

Page 152: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

© 2006 Antonio Carzaniga

Page 153: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort

© 2006 Antonio Carzaniga

Page 154: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort

© 2006 Antonio Carzaniga

Page 155: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort

© 2006 Antonio Carzaniga

Page 156: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort

© 2006 Antonio Carzaniga

Page 157: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

© 2006 Antonio Carzaniga

Page 158: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

Quick-Sort

© 2006 Antonio Carzaniga

Page 159: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

Quick-Sort Θ(n2) Θ(n log n) Θ(n log n) yes

© 2006 Antonio Carzaniga

Page 160: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

Quick-Sort Θ(n2) Θ(n log n) Θ(n log n) yes

Heap-Sort

© 2006 Antonio Carzaniga

Page 161: More on Sorting: Quick Sort and Heap Sort - inf.usi.ch fileMore on Sorting: Quick Sort and Heap Sort - inf.usi.ch

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

Insertion-Sort Θ(n2) Θ(n2) Θ(n) yes

Selection-Sort Θ(n2) Θ(n2) Θ(n2) yes

Bubble-Sort Θ(n2) Θ(n2) Θ(n2) yes

Merge-Sort Θ(n log n) Θ(n log n) Θ(n log n) no

Quick-Sort Θ(n2) Θ(n log n) Θ(n log n) yes

Heap-Sort Θ(n log n) Θ(n log n) Θ(n log n) yes

© 2006 Antonio Carzaniga