More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm...

161
More on Sorting: Quick Sort and Heap Sort Antonio Carzaniga Faculty of Informatics Università della Svizzera italiana March 12, 2020

Transcript of More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm...

Page 1: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

More on Sorting:

Quick Sort and Heap Sort

Antonio Carzaniga

Faculty of InformaticsUniversità della Svizzera italiana

March 12, 2020

Page 2: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Outline

Another divide-and-conquer sorting algorithm

The heap

Heap sort

Page 3: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Sorting Algorithms Seen So Far

Page 4: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

Page 5: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

INSERTION-SORT

Page 6: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

INSERTION-SORT Θ(n2) Θ(n2) Θ(n) yes

Page 7: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

INSERTION-SORT Θ(n2) Θ(n2) Θ(n) yes

SELECTION-SORT

Page 8: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Sorting Algorithms Seen So Far

Algorithm Complexity In place?

worst average best

INSERTION-SORT Θ(n2) Θ(n2) Θ(n) yes

SELECTION-SORT Θ(n2) Θ(n2) Θ(n2) yes

Page 9: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 10: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 11: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 12: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 13: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 14: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 15: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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〉

Page 16: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 17: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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〉

Page 18: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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〉

Page 19: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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〉

Page 20: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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?

Page 21: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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?

Page 22: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Another Strategy for Sorting

Problem: sorting

Page 23: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 24: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 25: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 26: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 27: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 28: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 29: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 30: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 31: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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]

Page 32: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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]

Page 33: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Another Divide-and-Conquer for Sorting

Divide:

Page 34: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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]

Page 35: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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:

Page 36: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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]

Page 37: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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:

Page 38: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 withMERGESORT

Page 39: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 withMERGESORT

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Page 40: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Partition

Page 41: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition and shift q to the right

Page 42: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition and shift q to the right

Loop invariant

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

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

Page 43: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 44: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 45: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 46: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 47: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 48: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 49: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 50: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 51: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 52: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 53: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 54: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 55: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 56: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 57: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 58: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 59: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 60: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 61: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 62: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 63: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 64: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 65: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 66: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 67: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 68: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 69: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 70: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 71: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 72: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 qposition 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

Page 73: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complete QUICKSORT Algorithm

PARTITION(A, begin, end)

1 q = begin2 v = A[end]3 for i = begin to end4 if A[i] ≤ v5 swap A[i] and A[q]6 q = q + 17 return q − 1

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Page 74: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of PARTITION

PARTITION(A, begin, end)

1 q = begin2 v = A[end]3 for i = begin to end4 if A[i] ≤ v5 swap A[i] and A[q]6 q = q + 17 return q − 1

Page 75: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of PARTITION

PARTITION(A, begin, end)

1 q = begin2 v = A[end]3 for i = begin to end4 if A[i] ≤ v5 swap A[i] and A[q]6 q = q + 17 return q − 1

T(n) = Θ(n)

Page 76: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Page 77: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Worst case

Page 78: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Worst case

◮ q = begin or q = end

Page 79: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT

QUICKSORT(A, begin, end)

1 if begin < end2 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

Page 80: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT

QUICKSORT(A, begin, end)

1 if begin < end2 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)

Page 81: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT

QUICKSORT(A, begin, end)

1 if begin < end2 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)

Page 82: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT (2)

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Page 83: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT (2)

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Best case

Page 84: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT (2)

QUICKSORT(A, begin, end)

1 if begin < end2 q = PARTITION(A, begin, end)3 QUICKSORT(A, begin, q − 1)4 QUICKSORT(A, q + 1, end)

Best case

◮ q = ⌈n/2⌉

Page 85: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT (2)

QUICKSORT(A, begin, end)

1 if begin < end2 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

Page 86: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT (2)

QUICKSORT(A, begin, end)

1 if begin < end2 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)

Page 87: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Complexity of QUICKSORT (2)

QUICKSORT(A, begin, end)

1 if begin < end2 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)

Page 88: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Sorting Algorithms Seen So Far

Page 89: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 90: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 91: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 92: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 93: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap

Page 94: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap

Our first real data structure

Page 95: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap

Our first real data structure

Interface

Page 96: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 97: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 98: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 99: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 100: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 101: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 102: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 103: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Structure

Page 104: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Structure

Conceptually a full binary tree

Page 105: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Structure

Conceptually a full binary tree

Page 106: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Structure

Conceptually a full binary tree

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 107: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 108: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 109: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 110: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Properties

Page 111: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Properties

Page 112: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 113: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Binary Heap: Properties

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 114: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 2iRIGHT(i)

return 2i + 1

Page 115: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 2iRIGHT(i)

return 2i + 1

1 2 3 4 5 6 7 8 9 101112131415

Page 116: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 2iRIGHT(i)

return 2i + 1

1 2 3 4 5 6 7 8 9 101112131415

Page 117: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 2iRIGHT(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]

Page 118: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Example

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

Page 119: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Example

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

E.g.,

Page 120: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 121: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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?

Page 122: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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?

Page 123: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Heap-Extract-Max

HEAP-EXTRACT-MAX procedure

◮ extract the max key

◮ rearrange the heap to maintain the max-heap property

Page 124: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 125: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 126: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 127: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 where the max-heap property holds

Page 128: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Max-Heapify

MAX-HEAPIFY(A, i) procedure

◮ assume: the max-heap property holds in the subtrees of node i

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

Page 129: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Max-Heapify

MAX-HEAPIFY(A, i) procedure

◮ assume: the max-heap property holds in the subtrees of node i

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

8

21 11

13 20 8 5

13 2 4 15

Page 130: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Max-Heapify

MAX-HEAPIFY(A, i) procedure

◮ assume: the max-heap property holds in the subtrees of node i

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

21

8 11

13 20 8 5

13 2 4 15

Page 131: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Max-Heapify

MAX-HEAPIFY(A, i) procedure

◮ assume: the max-heap property holds in the subtrees of node i

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

21

20 11

13 8 8 5

13 2 4 15

Page 132: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Max-Heapify

MAX-HEAPIFY(A, i) procedure

◮ assume: the max-heap property holds in the subtrees of node i

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

21

20 11

13 15 8 5

13 2 4 8

Page 133: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Max-Heapify

MAX-HEAPIFY(A, i) procedure

◮ assume: the max-heap property holds in the subtrees of node i

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

21

20 11

13 15 8 5

13 2 4 8

Page 134: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 = l5 else largest = i6 if r ≤ A.heap-size and A[r] > A[largest]7 largest = r8 if largest , i9 swap A[i] and A[largest]10 MAX-HEAPIFY(A, largest)

Page 135: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 = l5 else largest = i6 if r ≤ A.heap-size and A[r] > A[largest]7 largest = r8 if largest , i9 swap A[i] and A[largest]10 MAX-HEAPIFY(A, largest)

Complexity ofMAX-HEAPIFY?

Page 136: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 = l5 else largest = i6 if r ≤ A.heap-size and A[r] > A[largest]7 largest = r8 if largest , i9 swap A[i] and A[largest]10 MAX-HEAPIFY(A, largest)

Complexity ofMAX-HEAPIFY? The height of the tree!

Page 137: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 = l5 else largest = i6 if r ≤ A.heap-size and A[r] > A[largest]7 largest = r8 if largest , i9 swap A[i] and A[largest]10 MAX-HEAPIFY(A, largest)

Complexity ofMAX-HEAPIFY? The height of the tree!

T(n) = Θ(log n)

Page 138: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

Page 139: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 MAX-HEAPIFY(A, i)

Page 140: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 MAX-HEAPIFY(A, i)

Page 141: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 MAX-HEAPIFY(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

Page 142: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 MAX-HEAPIFY(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆

Page 143: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 MAX-HEAPIFY(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆ • •

• • •

Page 144: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 MAX-HEAPIFY(A, i)

length(A) = 10

1

2 3

4 5 6 7

8 9 10

⋆ ⋆

⋆ ⋆ • •

• • •

⋆ heapify points• leaves

Page 145: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 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

Page 146: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Building a Heap

BUILD-MAX-HEAP(A)

1 A.heap-size = length(A)2 for i = ⌊length(A)/2⌋ downto 13 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

⋆ ⋆ ⋆ ⋆ ⋆ • • • • •

Page 147: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Heap Sort

Idea: we can use a heap to sort an array

Page 148: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 13 swap A[i] and A[1]4 A.heap-size = A.heap-size − 15 MAX-HEAPIFY(A, 1)

Page 149: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 13 swap A[i] and A[1]4 A.heap-size = A.heap-size − 15 MAX-HEAPIFY(A, 1)

What is the complexity of HEAP-SORT?

Page 150: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 13 swap A[i] and A[1]4 A.heap-size = A.heap-size − 15 MAX-HEAPIFY(A, 1)

What is the complexity of HEAP-SORT?

T(n) = Θ(n log n)

Page 151: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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 13 swap A[i] and A[1]4 A.heap-size = A.heap-size − 15 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)

Page 152: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Summary of Sorting Algorithms

Page 153: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

INSERTION-SORT

Page 154: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

Summary of Sorting Algorithms

Algorithm Complexity In place?

worst average best

INSERTION-SORT Θ(n2) Θ(n2) Θ(n) yes

SELECTION-SORT

Page 155: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 156: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 157: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 158: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 159: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 160: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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

Page 161: More on Sorting: Quick Sort and Heap Sort - USI Informatics · Using the Partitioning Algorithm Basic step: partition A in three parts based on a chosen value v ∈ A AL contains

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