INF421, Lecture 7 Sorting Leo Liberti LIX, Ecole Polytechnique, - - PowerPoint PPT Presentation

inf421 lecture 7 sorting
SMART_READER_LITE
LIVE PREVIEW

INF421, Lecture 7 Sorting Leo Liberti LIX, Ecole Polytechnique, - - PowerPoint PPT Presentation

INF421, Lecture 7 Sorting Leo Liberti LIX, Ecole Polytechnique, France INF421, Lecture 7 p. 1/42 Course Objective : teach notions AND develop intelligence Evaluation : TP not en salle info, Contrle la fin. Note: max( CC, 3 4 CC +


slide-1
SLIDE 1

INF421, Lecture 7 Sorting

Leo Liberti LIX, ´ Ecole Polytechnique, France

INF421, Lecture 7 – p. 1/42

slide-2
SLIDE 2

Course

Objective: teach notions AND develop intelligence Evaluation: TP noté en salle info, Contrôle à la fin. Note:

max(CC, 3

4CC + 1 4TP)

Organization: fri 31/8, 7/9, 14/9, 21/9, 28/9, 5/10, 12/10, 19/10, 26/10,

amphi 1030-12 (Arago), TD 1330-1530, 1545-1745 (SI:30-34)

Books:

  • 1. K. Mehlhorn & P

. Sanders, Algorithms and Data Structures, Springer, 2008

  • 2. D. Knuth, The Art of Computer Programming, Addison-Wesley, 1997
  • 3. G. Dowek, Les principes des langages de programmation, Editions de l’X, 2008
  • 4. Ph. Baptiste & L. Maranget, Programmation et Algorithmique, Ecole Polytechnique

(Polycopié), 2006 Website: www.enseignement.polytechnique.fr/informatique/INF421 Blog: inf421.wordpress.com Contact: liberti@lix.polytechnique.fr (e-mail subject: INF421)

INF421, Lecture 7 – p. 2/42

slide-3
SLIDE 3

Lecture summary

Sorting complexity in general Mergesort Quicksort Two-way partition

INF421, Lecture 7 – p. 3/42

slide-4
SLIDE 4

The minimal knowledge

mergeSort(s1, . . . , sn)

m = ⌊ n

2 ⌋;

s′ = mergeSort(s1, . . . , sm); s′′ = mergeSort(sm+1, . . . , sn); merge s′, s′′ such that result ¯ s is sorted; return ¯ s; Split in half, recurse on shorter subsequences, then do some work to reassemble them

quickSort(s1, . . . , sn)

choose a k ≤ n; s′ = (si | i = k ∧ si < sk); s′′ = (si | i = k ∧ si ≥ sk); return (quickSort(s′), sk, quickSort(s′′)); Choose a value sk, split s.t. left

  • subseq. has values < sk, right
  • subseq. has values ≥ sk, re-

curse on subseq.

twoWaySort(s1, . . . , sn) ∈ {0, 1}n

i = 1; j = n while i ≤ j do

if si = 0 them i ← i + 1 else if sj = 1 then j ← j − 1 else swap si, sj; i++; j-- endif

end while Only applies to binary se- quences. Move i to leftmost 1 and j to rightmost 0. These are out of place, so swap them; continue until i, j meet

INF421, Lecture 7 – p. 4/42

slide-5
SLIDE 5

The sorting problem

Consider the following problem:

SORTING PROBLEM (SP). Given a sequence s

= (s1, . . . , sn), find a permutation π ∈ Sn of n symbols

with the following property:

∀1 ≤ i < j ≤ n (sπ(i) ≤ sπ(j)),

where Sn is the symmetric group of order n

INF421, Lecture 7 – p. 5/42

slide-6
SLIDE 6

The sorting problem

Consider the following problem:

SORTING PROBLEM (SP). Given a sequence s

= (s1, . . . , sn), find a permutation π ∈ Sn of n symbols

with the following property:

∀1 ≤ i < j ≤ n (sπ(i) ≤ sπ(j)),

where Sn is the symmetric group of order n In other words, order s

INF421, Lecture 7 – p. 5/42

slide-7
SLIDE 7

The sorting problem

Consider the following problem:

SORTING PROBLEM (SP). Given a sequence s

= (s1, . . . , sn), find a permutation π ∈ Sn of n symbols

with the following property:

∀1 ≤ i < j ≤ n (sπ(i) ≤ sπ(j)),

where Sn is the symmetric group of order n In other words, order s Type of s influences efficiency: the more generic, the less efficient. E.g. mergeSort and quickSort OK for all types; twoWaySort only OK for boolean

INF421, Lecture 7 – p. 5/42

slide-8
SLIDE 8

Problem complexity

Algorithmic complexity : worst-case run time over all inputs Problem complexity : worst case run time of most efficient algorithm for problem Usually: upper bound on problem complexity Given problem P, find an O(f) algorithm, say com- plexity of P is no worse than O(f) Lower bounds? Given problem P, show that no algorithm for P can ever be better than Ω(f) Seems to require listing all possible algorithms for P An ill-defined question?

INF421, Lecture 7 – p. 6/42

slide-9
SLIDE 9

Comparisons

Sorting algorithms are comparison-based

given si, sj, does si ≤ sj hold?

INF421, Lecture 7 – p. 7/42

slide-10
SLIDE 10

Comparisons

Sorting algorithms are comparison-based

given si, sj, does si ≤ sj hold?

Describe any sorting algorithm by tracing calls to comparisons

sorting tree

INF421, Lecture 7 – p. 7/42

slide-11
SLIDE 11

Comparisons

Sorting algorithms are comparison-based

given si, sj, does si ≤ sj hold?

Describe any sorting algorithm by tracing calls to comparisons

sorting tree

E.g. sorting tree to order s1, s2, s3:

1 2 2 3 3 1 3 2 1 3

s1

?

≤ s2 s1

?

≤ s3 s1

?

≤ s3 s2

?

≤ s3 s2

?

≤ s3 e (23) (132) (12) (123) (13)

INF421, Lecture 7 – p. 7/42

slide-12
SLIDE 12

Sorting trees

Sorting tree: different inputs ⇒ different paths root→leaf

INF421, Lecture 7 – p. 8/42

slide-13
SLIDE 13

Sorting trees

Sorting tree: different inputs ⇒ different paths root→leaf

Encodes behaviour of comparison-based (CB) sorting algorithm over all inputs

INF421, Lecture 7 – p. 8/42

slide-14
SLIDE 14

Sorting trees

Sorting tree: different inputs ⇒ different paths root→leaf

Encodes behaviour of comparison-based (CB) sorting algorithm over all inputs

∃ mapping Γ:CB sorting algorithms → sorting trees

INF421, Lecture 7 – p. 8/42

slide-15
SLIDE 15

Sorting trees

Sorting tree: different inputs ⇒ different paths root→leaf

Encodes behaviour of comparison-based (CB) sorting algorithm over all inputs

∃ mapping Γ:CB sorting algorithms → sorting trees

Two CB sorting algorithms with same sorting tree behave the same way

INF421, Lecture 7 – p. 8/42

slide-16
SLIDE 16

Sorting trees

Sorting tree: different inputs ⇒ different paths root→leaf

Encodes behaviour of comparison-based (CB) sorting algorithm over all inputs

∃ mapping Γ:CB sorting algorithms → sorting trees

Two CB sorting algorithms with same sorting tree behave the same way

⇒ Assume WLOG Γ is 1-1 ⇒ | dom Γ| ≤ | ran Γ|

INF421, Lecture 7 – p. 8/42

slide-17
SLIDE 17

Sorting trees

Sorting tree: different inputs ⇒ different paths root→leaf

Encodes behaviour of comparison-based (CB) sorting algorithm over all inputs

∃ mapping Γ:CB sorting algorithms → sorting trees

Two CB sorting algorithms with same sorting tree behave the same way

⇒ Assume WLOG Γ is 1-1 ⇒ | dom Γ| ≤ | ran Γ|

At most |sorting trees| CB sorting algorithms

INF421, Lecture 7 – p. 8/42

slide-18
SLIDE 18

Sorting trees

Sorting tree: different inputs ⇒ different paths root→leaf

Encodes behaviour of comparison-based (CB) sorting algorithm over all inputs

∃ mapping Γ:CB sorting algorithms → sorting trees

Two CB sorting algorithms with same sorting tree behave the same way

⇒ Assume WLOG Γ is 1-1 ⇒ | dom Γ| ≤ | ran Γ|

At most |sorting trees| CB sorting algorithms

Also: best possible CB sorting algorithm = best possible

sorting tree

INF421, Lecture 7 – p. 8/42

slide-19
SLIDE 19

Best worst-case complexity

Tn = set of all sorting trees for n-sequences

INF421, Lecture 7 – p. 9/42

slide-20
SLIDE 20

Best worst-case complexity

Tn = set of all sorting trees for n-sequences Different inputs ⇒ different ordering permutations π in leaves

INF421, Lecture 7 – p. 9/42

slide-21
SLIDE 21

Best worst-case complexity

Tn = set of all sorting trees for n-sequences Different inputs ⇒ different ordering permutations π in leaves For T ∈ Tn and π ∈ Sn,

ℓ(T, π) = length of path root→leaf(π) in T

INF421, Lecture 7 – p. 9/42

slide-22
SLIDE 22

Best worst-case complexity

Tn = set of all sorting trees for n-sequences Different inputs ⇒ different ordering permutations π in leaves For T ∈ Tn and π ∈ Sn,

ℓ(T, π) = length of path root→leaf(π) in T ℓ(T, π): trace length of a CB sorting algorithm on given input

upper bound on ℓ over π = worst-case complexity of alg.

INF421, Lecture 7 – p. 9/42

slide-23
SLIDE 23

Best worst-case complexity

Tn = set of all sorting trees for n-sequences Different inputs ⇒ different ordering permutations π in leaves For T ∈ Tn and π ∈ Sn,

ℓ(T, π) = length of path root→leaf(π) in T ℓ(T, π): trace length of a CB sorting algorithm on given input

upper bound on ℓ over π = worst-case complexity of alg.

Best worst-case complexity is, for each n ≥ 0:

Bn = min

T∈Tn max π∈Sn ℓ(T, π).

INF421, Lecture 7 – p. 9/42

slide-24
SLIDE 24

Best worst-case complexity

Tn = set of all sorting trees for n-sequences Different inputs ⇒ different ordering permutations π in leaves For T ∈ Tn and π ∈ Sn,

ℓ(T, π) = length of path root→leaf(π) in T ℓ(T, π): trace length of a CB sorting algorithm on given input

upper bound on ℓ over π = worst-case complexity of alg.

Best worst-case complexity is, for each n ≥ 0:

Bn = min

T∈Tn max π∈Sn ℓ(T, π).

INF421, Lecture 7 – p. 9/42

slide-25
SLIDE 25

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

INF421, Lecture 7 – p. 10/42

slide-26
SLIDE 26

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

INF421, Lecture 7 – p. 10/42

slide-27
SLIDE 27

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

A binary tree T with depth ≤ k has |V (T)| ≤ 2k

INF421, Lecture 7 – p. 10/42

slide-28
SLIDE 28

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

A binary tree T with depth ≤ k has |V (T)| ≤ 2k ⇒ The sorting tree T ∗ of best algorithm has |V (T ∗)| ≤ 2Bn

  • INF421, Lecture 7 – p. 10/42
slide-29
SLIDE 29

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

A binary tree T with depth ≤ k has |V (T)| ≤ 2k ⇒ The sorting tree T ∗ of best algorithm has |V (T ∗)| ≤ 2Bn

  • ∀T ∈ Tn, each π ∈ Sn appears in a leaf node of T

INF421, Lecture 7 – p. 10/42

slide-30
SLIDE 30

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

A binary tree T with depth ≤ k has |V (T)| ≤ 2k ⇒ The sorting tree T ∗ of best algorithm has |V (T ∗)| ≤ 2Bn

  • ∀T ∈ Tn, each π ∈ Sn appears in a leaf node of T

⇒ Any T ∈ Tn has at least n! leaf nodes, i.e. |V (T)| ≥ n!

INF421, Lecture 7 – p. 10/42

slide-31
SLIDE 31

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

A binary tree T with depth ≤ k has |V (T)| ≤ 2k ⇒ The sorting tree T ∗ of best algorithm has |V (T ∗)| ≤ 2Bn

  • ∀T ∈ Tn, each π ∈ Sn appears in a leaf node of T

⇒ Any T ∈ Tn has at least n! leaf nodes, i.e. |V (T)| ≥ n! Hence, n! ≤ 2Bn, which implies Bn ≥ ⌈log n!⌉

INF421, Lecture 7 – p. 10/42

slide-32
SLIDE 32

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

A binary tree T with depth ≤ k has |V (T)| ≤ 2k ⇒ The sorting tree T ∗ of best algorithm has |V (T ∗)| ≤ 2Bn

  • ∀T ∈ Tn, each π ∈ Sn appears in a leaf node of T

⇒ Any T ∈ Tn has at least n! leaf nodes, i.e. |V (T)| ≥ n! Hence, n! ≤ 2Bn, which implies Bn ≥ ⌈log n!⌉ By Stirling’s approx., log n! = n log n −

1 ln 2n + O(log n)

  • INF421, Lecture 7 – p. 10/42
slide-33
SLIDE 33

The complexity of sorting

For any tree T, |V (T)| = number of nodes of T

Tree depth: max. path length root→leaf in tree

A binary tree T with depth ≤ k has |V (T)| ≤ 2k ⇒ The sorting tree T ∗ of best algorithm has |V (T ∗)| ≤ 2Bn

  • ∀T ∈ Tn, each π ∈ Sn appears in a leaf node of T

⇒ Any T ∈ Tn has at least n! leaf nodes, i.e. |V (T)| ≥ n! Hence, n! ≤ 2Bn, which implies Bn ≥ ⌈log n!⌉ By Stirling’s approx., log n! = n log n −

1 ln 2n + O(log n)

  • ⇒ Bn is bounded below by a function proportional to n log n

(we say Bn is Ω(n log n))

INF421, Lecture 7 – p. 10/42

slide-34
SLIDE 34

Today’s magic result: first part

Complexity of sorting: Ω(n log n)

INF421, Lecture 7 – p. 11/42

slide-35
SLIDE 35

Simple sorting algorithms

INF421, Lecture 7 – p. 12/42

slide-36
SLIDE 36

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence

INF421, Lecture 7 – p. 13/42

slide-37
SLIDE 37

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

INF421, Lecture 7 – p. 13/42

slide-38
SLIDE 38

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2), ∅

INF421, Lecture 7 – p. 13/42

slide-39
SLIDE 39

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

→ (3, 4, 2 ), (1)

INF421, Lecture 7 – p. 13/42

slide-40
SLIDE 40

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

→ ( 3 , 4), (1, 2)

INF421, Lecture 7 – p. 13/42

slide-41
SLIDE 41

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

→ ( 4 ), (1, 2, 3)

INF421, Lecture 7 – p. 13/42

slide-42
SLIDE 42

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

→ (1, 2, 3, 4)

INF421, Lecture 7 – p. 13/42

slide-43
SLIDE 43

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

INF421, Lecture 7 – p. 13/42

slide-44
SLIDE 44

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

( 3 , 1, 4, 2)

INF421, Lecture 7 – p. 13/42

slide-45
SLIDE 45

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

→ ( 1 , 4, 2), (3)

INF421, Lecture 7 – p. 13/42

slide-46
SLIDE 46

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

→ ( 4 , 2), (1, 3)

INF421, Lecture 7 – p. 13/42

slide-47
SLIDE 47

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

→ ( 2 ), (1, 3, 4)

INF421, Lecture 7 – p. 13/42

slide-48
SLIDE 48

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

→ (1, 2, 3, 4)

INF421, Lecture 7 – p. 13/42

slide-49
SLIDE 49

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

( 3 , 1, 4, 2) → ( 1 , 4, 2), (3) → ( 4 , 2), (1, 3) → ( 2 ), (1, 3, 4) → (1, 2, 3, 4)

INF421, Lecture 7 – p. 13/42

slide-50
SLIDE 50

Simple sorting algorithms

I shall save you the trouble of learning all the numerous types of sorting algorithms in existence Let me just mention selection sort, where you repeatedly select the minimum element of s,

(3, 1 , 4, 2) → (3, 4, 2 ), (1) → ( 3 , 4), (1, 2) → ( 4 ), (1, 2, 3) → (1, 2, 3, 4)

and insertion sort, where you insert the next element of s at its proper position in the sorted sequence

( 3 , 1, 4, 2) → ( 1 , 4, 2), (3) → ( 4 , 2), (1, 3) → ( 2 ), (1, 3, 4) → (1, 2, 3, 4)

Both are O(n2); insertion sort is fast for small |s|

INF421, Lecture 7 – p. 13/42

slide-51
SLIDE 51

Mergesort

INF421, Lecture 7 – p. 14/42

slide-52
SLIDE 52

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3)

INF421, Lecture 7 – p. 15/42

slide-53
SLIDE 53

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3)

INF421, Lecture 7 – p. 15/42

slide-54
SLIDE 54

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

INF421, Lecture 7 – p. 15/42

slide-55
SLIDE 55

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

INF421, Lecture 7 – p. 15/42

slide-56
SLIDE 56

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9)

INF421, Lecture 7 – p. 15/42

slide-57
SLIDE 57

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3,5,6) ( 1 ,3,4,9) → ∅

INF421, Lecture 7 – p. 15/42

slide-58
SLIDE 58

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

( 2 ,3,5,6) (1,3,4,9) → (1)

INF421, Lecture 7 – p. 15/42

slide-59
SLIDE 59

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2, 3 ,5,6) (1,3,4,9) → (1, 2)

INF421, Lecture 7 – p. 15/42

slide-60
SLIDE 60

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3,5,6) (1, 3 ,4,9) → (1, 2, 3)

INF421, Lecture 7 – p. 15/42

slide-61
SLIDE 61

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3,5,6) (1,3, 4 ,9) → (1, 2, 3, 3)

INF421, Lecture 7 – p. 15/42

slide-62
SLIDE 62

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3, 5 ,6) (1,3,4,9) → (1, 2, 3, 3, 4)

INF421, Lecture 7 – p. 15/42

slide-63
SLIDE 63

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3,5, 6 ) (1,3,4,9) → (1, 2, 3, 3, 4, 5)

INF421, Lecture 7 – p. 15/42

slide-64
SLIDE 64

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3,5,6) (1,3,4, 9 ) → (1, 2, 3, 3, 4, 5, 6)

INF421, Lecture 7 – p. 15/42

slide-65
SLIDE 65

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3,5,6) (1,3,4,9) → (1, 2, 3, 3, 4, 5, 6, 9) = ¯

s

INF421, Lecture 7 – p. 15/42

slide-66
SLIDE 66

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Split s midway: s′ = (5, 3, 6, 2), s′′ = (1, 9, 4, 3) Sort s′, s′′: |s′| < |s| and |s′′| < |s| ⇒ use recursion

Base case: If |s| ≤ 1 then s already sorted by definition

Get s′ = (2, 3, 5, 6) and s′′ = (1, 3, 4, 9) Merge s′, s′′ into a sorted sequence ¯

s:

(2,3,5,6) (1,3,4,9) → (1, 2, 3, 3, 4, 5, 6, 9) = ¯

s

Return ¯

s

INF421, Lecture 7 – p. 15/42

slide-67
SLIDE 67

Merge

merge(s′, s′′): merges two sorted sequences s′, s′′ in a sorted sequence containing all elements in s′, s′′

INF421, Lecture 7 – p. 16/42

slide-68
SLIDE 68

Merge

merge(s′, s′′): merges two sorted sequences s′, s′′ in a sorted sequence containing all elements in s′, s′′ Since s′, s′′ are both already sorted, merging them so that the output is sorted is efficient Read first (and smallest) elements of s′, s′′: O(1) Compare these two elements: O(1) There are |s| elements to process: O(n)

INF421, Lecture 7 – p. 16/42

slide-69
SLIDE 69

Merge

merge(s′, s′′): merges two sorted sequences s′, s′′ in a sorted sequence containing all elements in s′, s′′ Since s′, s′′ are both already sorted, merging them so that the output is sorted is efficient Read first (and smallest) elements of s′, s′′: O(1) Compare these two elements: O(1) There are |s| elements to process: O(n) You can implement this using lists: if s′ is empty return s′′, if s′′ is empty return s′, and otherwise compare the first elements of both and choose smallest

INF421, Lecture 7 – p. 16/42

slide-70
SLIDE 70

Recursive algorithm

mergeSort(s) {

1: if |s| ≤ 1 then 2:

return s;

3: else 4:

m = ⌊|s|

2 ⌋;

5:

s′ = mergeSort(e1, . . . , em);

6:

s′′ = mergeSort(em+1, . . . , en);

7:

return merge(s′, s′′);

8: end if

}

By INF311, mergeSort has worst-case complexity

O(n log n)

INF421, Lecture 7 – p. 17/42

slide-71
SLIDE 71

Today’s magic result: second part

Complexity of sorting: Θ(n log n)

A function is Θ(g(n)) if it is both O(g(n)) and Ω(g(n))

INF421, Lecture 7 – p. 18/42

slide-72
SLIDE 72

Quicksort

INF421, Lecture 7 – p. 19/42

slide-73
SLIDE 73

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3)

INF421, Lecture 7 – p. 20/42

slide-74
SLIDE 74

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1)

INF421, Lecture 7 – p. 20/42

slide-75
SLIDE 75

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3)

INF421, Lecture 7 – p. 20/42

slide-76
SLIDE 76

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3 , 6, 2, 1, 9, 4, 3) → ∅, ∅

INF421, Lecture 7 – p. 20/42

slide-77
SLIDE 77

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3), ∅

INF421, Lecture 7 – p. 20/42

slide-78
SLIDE 78

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6 , 2, 1, 9, 4, 3) → (3), ∅

INF421, Lecture 7 – p. 20/42

slide-79
SLIDE 79

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3), (6)

INF421, Lecture 7 – p. 20/42

slide-80
SLIDE 80

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2 , 1, 9, 4, 3) → (3), (6)

INF421, Lecture 7 – p. 20/42

slide-81
SLIDE 81

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2), (6)

INF421, Lecture 7 – p. 20/42

slide-82
SLIDE 82

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1 , 9, 4, 3) → (3, 2), (6)

INF421, Lecture 7 – p. 20/42

slide-83
SLIDE 83

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2, 1), (6)

INF421, Lecture 7 – p. 20/42

slide-84
SLIDE 84

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9 , 4, 3) → (3, 2, 1), (6)

INF421, Lecture 7 – p. 20/42

slide-85
SLIDE 85

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2, 1), (6, 9)

INF421, Lecture 7 – p. 20/42

slide-86
SLIDE 86

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4 , 3) → (3, 2, 1), (6, 9)

INF421, Lecture 7 – p. 20/42

slide-87
SLIDE 87

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2, 1, 4), (6, 9)

INF421, Lecture 7 – p. 20/42

slide-88
SLIDE 88

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3 ) → (3, 2, 1, 4), (6, 9)

INF421, Lecture 7 – p. 20/42

slide-89
SLIDE 89

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2, 1, 4, 3), (6, 9)

INF421, Lecture 7 – p. 20/42

slide-90
SLIDE 90

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2, 1, 4, 3), (6, 9)

Sort s′ = (3, 2, 1, 4, 3) and s′′ = (6, 9): since |s′| < |s| and

|s′′| < |s| we can use recursion; base case |s| ≤ 1

INF421, Lecture 7 – p. 20/42

slide-91
SLIDE 91

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2, 1, 4, 3), (6, 9)

Sort s′ = (3, 2, 1, 4, 3) and s′′ = (6, 9): since |s′| < |s| and

|s′′| < |s| we can use recursion; base case |s| ≤ 1

Update s to (s′, p, s′′)

INF421, Lecture 7 – p. 20/42

slide-92
SLIDE 92

Divide-and-conquer

Let s = (5, 3, 6, 2, 1, 9, 4, 3) Choose a pivot value p = s1 = 5 (no particular reason for choosing s1) Partition (s2, . . . , sn) in s′ (elements smaller than p) and

s′′ (elements greather than or equal to p): (5, 3, 6, 2, 1, 9, 4, 3) → (3, 2, 1, 4, 3), (6, 9)

Sort s′ = (3, 2, 1, 4, 3) and s′′ = (6, 9): since |s′| < |s| and

|s′′| < |s| we can use recursion; base case |s| ≤ 1

Update s to (s′, p, s′′)

Notice: in mergeSort, we recurse first, then work on subsequences

  • afterwards. In quickSort, we work on subsequences first, then recurse
  • n them afterwards

INF421, Lecture 7 – p. 20/42

slide-93
SLIDE 93

Partition

partition(s): produces two subsequences s′, s′′ of (s2, . . . , sn) such that: s′ = (si | i = 1 ∧ si < s1) s′′ = (si | i = 1 ∧ si ≥ s1)

INF421, Lecture 7 – p. 21/42

slide-94
SLIDE 94

Partition

partition(s): produces two subsequences s′, s′′ of (s2, . . . , sn) such that: s′ = (si | i = 1 ∧ si < s1) s′′ = (si | i = 1 ∧ si ≥ s1) Scan s: if si < s1 put si in s′, otherwise put it in s′′

INF421, Lecture 7 – p. 21/42

slide-95
SLIDE 95

Partition

partition(s): produces two subsequences s′, s′′ of (s2, . . . , sn) such that: s′ = (si | i = 1 ∧ si < s1) s′′ = (si | i = 1 ∧ si ≥ s1) Scan s: if si < s1 put si in s′, otherwise put it in s′′ There are |s| − 1 elements to process: O(n)

INF421, Lecture 7 – p. 21/42

slide-96
SLIDE 96

Partition

partition(s): produces two subsequences s′, s′′ of (s2, . . . , sn) such that: s′ = (si | i = 1 ∧ si < s1) s′′ = (si | i = 1 ∧ si ≥ s1) Scan s: if si < s1 put si in s′, otherwise put it in s′′ There are |s| − 1 elements to process: O(n) You can implement this using arrays; moreover, if you use a swap function such that, given i, j, swaps si with sj in s, you don’t even need to create any new temporary array: you can update s “in place”

INF421, Lecture 7 – p. 21/42

slide-97
SLIDE 97

Recursive algorithm

quickSort(s) {

1: if |s| ≤ 1 then 2:

return ;

3: else 4:

(s′, s′′) = partition(s);

5:

quickSort(s′);

6:

quickSort(s′′);

7:

s ← (s′, s1, s′′);

8: end if

}

INF421, Lecture 7 – p. 22/42

slide-98
SLIDE 98

Complexity

Worst-case complexity: O(n2) Average-case complexity: O(n log n) Very fast in practice

INF421, Lecture 7 – p. 23/42

slide-99
SLIDE 99

Worst-case complexity

Consider the input (n, n − 1, . . . , 1) with pivot s1

INF421, Lecture 7 – p. 24/42

slide-100
SLIDE 100

Worst-case complexity

Consider the input (n, n − 1, . . . , 1) with pivot s1 Recursion level 1: p = n, s′ = (n − 1, . . . , 1), s′′ = ∅

INF421, Lecture 7 – p. 24/42

slide-101
SLIDE 101

Worst-case complexity

Consider the input (n, n − 1, . . . , 1) with pivot s1 Recursion level 1: p = n, s′ = (n − 1, . . . , 1), s′′ = ∅ Recursion level 2: p = n − 1, s′ = (n − 2, . . . , 1), s′′ = ∅

INF421, Lecture 7 – p. 24/42

slide-102
SLIDE 102

Worst-case complexity

Consider the input (n, n − 1, . . . , 1) with pivot s1 Recursion level 1: p = n, s′ = (n − 1, . . . , 1), s′′ = ∅ Recursion level 2: p = n − 1, s′ = (n − 2, . . . , 1), s′′ = ∅ And so on, down to p = 1 (base case)

INF421, Lecture 7 – p. 24/42

slide-103
SLIDE 103

Worst-case complexity

Consider the input (n, n − 1, . . . , 1) with pivot s1 Recursion level 1: p = n, s′ = (n − 1, . . . , 1), s′′ = ∅ Recursion level 2: p = n − 1, s′ = (n − 2, . . . , 1), s′′ = ∅ And so on, down to p = 1 (base case) Each partitioning call takes O(n)

INF421, Lecture 7 – p. 24/42

slide-104
SLIDE 104

Worst-case complexity

Consider the input (n, n − 1, . . . , 1) with pivot s1 Recursion level 1: p = n, s′ = (n − 1, . . . , 1), s′′ = ∅ Recursion level 2: p = n − 1, s′ = (n − 2, . . . , 1), s′′ = ∅ And so on, down to p = 1 (base case) Each partitioning call takes O(n) Get O(n2)

INF421, Lecture 7 – p. 24/42

slide-105
SLIDE 105

2-Way partitioning

INF421, Lecture 7 – p. 25/42

slide-106
SLIDE 106

Definition by example

Input: (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Desired output: (0, 0, 0, 0, 0, 1, 1, 1, 1, 1)

INF421, Lecture 7 – p. 26/42

slide-107
SLIDE 107

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-108
SLIDE 108

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place)

INF421, Lecture 7 – p. 27/42

slide-109
SLIDE 109

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them

INF421, Lecture 7 – p. 27/42

slide-110
SLIDE 110

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter

INF421, Lecture 7 – p. 27/42

slide-111
SLIDE 111

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

( 1 , 0, 0, 1, 1, 0, 0, 0 , 1, 1)

INF421, Lecture 7 – p. 27/42

slide-112
SLIDE 112

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

(0, 0, 0, 1, 1, 0, 0, 1, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-113
SLIDE 113

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

(0, 0, 0, 1 , 1, 0, 0 , 1, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-114
SLIDE 114

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

(0, 0, 0, 0, 1, 0, 1, 1, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-115
SLIDE 115

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

(0, 0, 0, 0, 1 , 0 , 1, 1, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-116
SLIDE 116

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

(0, 0, 0, 0, 0, 1, 1, 1, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-117
SLIDE 117

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

(0, 0, 0, 0, 0, 1, 1, 1, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-118
SLIDE 118

Iterating swaps

Let s = (1, 0, 0, 1, 1, 0, 0, 0, 1, 1) Find leftmost 1 and rightmost 0 (these are out of place) Swap them Increase leftmost counter, decrease rightmost counter Repeat until counters become equal

(1, 0, 0, 1, 1, 0, 0, 0, 1, 1) → (0, 0, 0, 1, 1, 0, 0, 1, 1, 1) → (0, 0, 0, 0, 1, 0, 1, 1, 1, 1) → (0, 0, 0, 0, 0, 1, 1, 1, 1, 1)

INF421, Lecture 7 – p. 27/42

slide-119
SLIDE 119

The algorithm

i = 0; j = n − 1;

while i < j do if si = 0 then

i ← i + 1;

else if sj = 1 then

j ← j − 1;

else swap(s, i, j);

i ← i + 1; j ← j − 1;

end if end while

INF421, Lecture 7 – p. 28/42

slide-120
SLIDE 120

Worst-case complexity

Occurs with input (1, . . . , 1, 0, . . . , 0) where number of 1’s are around the same as the number of 0’s Requires ⌊n

2⌋ swaps

Worst-case O(n)

INF421, Lecture 7 – p. 29/42

slide-121
SLIDE 121

A paradox?

At the outset, we proved that sorting had complexity

Θ(n log n)

INF421, Lecture 7 – p. 30/42

slide-122
SLIDE 122

A paradox?

At the outset, we proved that sorting had complexity

Θ(n log n)

But 2-way partioning requires only O(n)

INF421, Lecture 7 – p. 30/42

slide-123
SLIDE 123

A paradox?

At the outset, we proved that sorting had complexity

Θ(n log n)

But 2-way partioning requires only O(n) Contradiction? Paradox?

INF421, Lecture 7 – p. 30/42

slide-124
SLIDE 124

A paradox?

At the outset, we proved that sorting had complexity

Θ(n log n)

But 2-way partioning requires only O(n) Contradiction? Paradox? Only apparent: the initial theorem was under the following assumptions:

no prior knowledge on the type of input (“general input”)

  • nly comparison-based algorithms are concerned

INF421, Lecture 7 – p. 30/42

slide-125
SLIDE 125

A paradox?

At the outset, we proved that sorting had complexity

Θ(n log n)

But 2-way partioning requires only O(n) Contradiction? Paradox? Only apparent: the initial theorem was under the following assumptions:

no prior knowledge on the type of input (“general input”)

  • nly comparison-based algorithms are concerned

Neither assumption is true for 2-way partitioning

we know that the input sequence is of binary type the algorithm never uses a comparison

INF421, Lecture 7 – p. 30/42

slide-126
SLIDE 126

Appendix [P. Cameron, Combinatorics]

INF421, Lecture 7 – p. 31/42

slide-127
SLIDE 127

Quicksort: average complexity 1/10

Let n = |s| Let qn be the average number of comparisons made by quickSort to sort an n-sequence partition(s) involves n − 1 comparisons Assume the pivot p = s1 is the k-th smallest element of s Then, recursion takes qk−1 + qn−k comparisons on average Average this over the n values that k can take This implies:

qn = n − 1 + 1 n

n

  • k=1

(qk−1 + qn−k)

(1)

INF421, Lecture 7 – p. 32/42

slide-128
SLIDE 128

Quicksort: average complexity 2/10

Notice that in the sum n

k=1(qk−1 + qn−k), each qk

  • ccurs twice

k qk−1 qn−k 1 q0 qn−1 2 q1 qn−2

. . . . . . . . .

n − 1 qn−2 q1 n qn−1 q0

Hence we can write:

qn = n − 1 + 2 n

n−1

  • k=0

qk

(2)

INF421, Lecture 7 – p. 33/42

slide-129
SLIDE 129

Quicksort: average complexity 3/10

Equation (2) is a recurrence relation A solution of a recurrence relation is a closed-form expression for qn which does not include the symbol qk for any integer k ≥ 0 One solution method consists in writing the solution as the infinite sequence (q0, q1, q2, . . . , qn, . . .) as a formal

power series:

Q(t) =

  • n≥0

qntn

(3)

If Q(t) is known, then the value for each qn can also be

  • btained:

Differentiate Q(t) n times with respect to t, set t = 0, and divide the result by n!

INF421, Lecture 7 – p. 34/42

slide-130
SLIDE 130

Quicksort: average complexity 4/10

Multiply each side of the recurrence relation (2) by ntn and sum over all n ≥ 0, get:

  • n≥0

nqntn =

  • n≥0

n(n − 1)tn + 2

  • n≥0

n−1

  • k=0

qk

  • tn

(4)

We now replace each of these three terms so as to be able to derive a more convenient expression for Q(t)

INF421, Lecture 7 – p. 35/42

slide-131
SLIDE 131

Quicksort: average complexity 5/10

Differentiate Q(t) with respect to t and multiply by t to get an expression for the first term: tdQ(t) dt = t

  • n≥0

nqntn−1 =

  • n≥0

nqntn,

(5)

For the second term: by lecture 1,

n≥0 tn = 1 1−t

Differentiate this equation twice with respect to t, we get:

  • n≥0

n(n − 1)tn−2 = 2 (1 − t)3

(6)

Now multiply both members by t2 to get an expression for the second term:

  • n≥0

n(n − 1)tn = 2t2 (1 − t)3

(7)

INF421, Lecture 7 – p. 36/42

slide-132
SLIDE 132

Quicksort: average complexity 6/10

Now for the third: the n-th term of the sum

  • n≥0(n−1

k=0 qk)tn can be written as n−1

  • k=0

tn−k(qktk)

Hence, the whole sum over n can be written as the following product (convince yourself that this is true):

(t + t2 + t3 + . . .)(q0 + q1t + q2t2 + q3t3 + . . .)

The first factor is

n≥0 tn = 1 1−t, and the second is

simply the expression for Q(t) Hence, the third term is 2tQ(t)

1−t

INF421, Lecture 7 – p. 37/42

slide-133
SLIDE 133

Quicksort: average complexity 7/10

Putting it all together, we obtain a first-order differential equation for Q(t): tQ′(t) = 2t2 (1 − t)3 + 2t 1 − tQ(t)

(8)

Remark that if we differentiate the expression (1 − t)2Q(t) (which I pulled

  • ut of a hat) w.r.t. t, we get:

d dt((1 − t)2Q(t)) = (1 − t)2Q′(t) − 2(1 − t)Q(t)

(9)

We rearrange the terms of Eq. (8) to get: tQ′(t) − 2t 1 − tQ(t) = 2t2 (1 − t)3

(10)

We multiply Eq. (10) through by (1−t)2

t

and get: (1 − t)2Q′(t) − 2(1 − t)Q(t) = 2t 1 − t

(11)

INF421, Lecture 7 – p. 38/42

slide-134
SLIDE 134

Quicksort: average complexity 8/10

The RHS of Eq. (9) is the same as the LHS of Eq. (11), hence we can rewrite Eq. 9 as:

d dt((1 − t)2Q(t)) = 2t 1 − t

(12)

Now, straightforward integration w.r.t. t yields:

Q(t) = −2(t + log(1 − t)) (1 − t)2

(13)

INF421, Lecture 7 – p. 39/42

slide-135
SLIDE 135

Quicksort: average complexity 9/10

The next step consists in writing the power series for log and 1/(1 − t)2, rearrange them in a product, and read off the coefficient qn of the term in tn. Without going into details, this yields:

qn = 2(n + 1)

n

  • k=1

1 k − 4n

(14)

for all n ≥ 0 For all n ≥ 0, the term n

k=1 1 k is an approximation of:

n

1

1 xdx = log(n) + O(1)

(15)

INF421, Lecture 7 – p. 40/42

slide-136
SLIDE 136

Quicksort: average complexity 10/10

Finally, we get an asymptotic expression for qn:

∀n ≥ 0 qn = 2n log(n) + O(n)

(16)

This shows that the average number of comparisons taken by quickSort is O(n log n)

INF421, Lecture 7 – p. 41/42

slide-137
SLIDE 137

End of Lecture 4

INF421, Lecture 7 – p. 42/42