Complexity of the Adaptive ShiversSort Algorithm and of its sibling - - PowerPoint PPT Presentation

complexity of the adaptive shiverssort algorithm
SMART_READER_LITE
LIVE PREVIEW

Complexity of the Adaptive ShiversSort Algorithm and of its sibling - - PowerPoint PPT Presentation

Complexity of the Adaptive ShiversSort Algorithm and of its sibling TimSort Vincent Jug LIGM Universit Paris-Est Marne-la-Valle, ESIEE, ENPC & CNRS 18/03/2019 V. Jug Complexity of the Adaptive ShiversSort Algorithm Contents


slide-1
SLIDE 1

Complexity of the Adaptive ShiversSort Algorithm

and of its sibling TimSort

Vincent Jugé

LIGM – Université Paris-Est Marne-la-Vallée, ESIEE, ENPC & CNRS

18/03/2019

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-2
SLIDE 2

Contents

1

Efficient Merge Sorts

2

TimSort

3

Adaptive ShiversSort

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-3
SLIDE 3

Sorting data

1 4 3 1 5 4 3 2 2 2 1 1 2 2 2 3 3 4 4 5

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-4
SLIDE 4

Sorting data – in a stable manner

01 11 41 31 12 51 42 32 21 22 02 23 · · · · · · · · · · · · · · · 01 02 11 12 21 22 23 31 32 41 42 51

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-5
SLIDE 5

Sorting data – in a stable manner

01 11 41 31 12 51 42 32 21 22 02 23 01 02 11 12 21 22 23 31 32 41 42 51 MergeSort has a worst-case time complexity of O(n log(n))

Can we do better?

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-6
SLIDE 6

Sorting data – in a stable manner

01 11 41 31 12 51 42 32 21 22 02 23 01 02 11 12 21 22 23 31 32 41 42 51 MergeSort has a worst-case time complexity of O(n log(n))

Can we do better? No!

Proof: There are n! possible reorderings Each element comparison gives a 1-bit information Thus log2(n!) ∼ n log2(n) tests are required

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-7
SLIDE 7

Sorting data – in a stable manner

01 11 41 31 12 51 42 32 21 22 02 23 01 02 11 12 21 22 23 31 32 41 42 51 MergeSort has a worst-case time complexity of O(n log(n))

Can we do better? No!

Proof: There are n! possible reorderings Each element comparison gives a 1-bit information Thus log2(n!) ∼ n log2(n) tests are required E N D O F T A L K !

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-8
SLIDE 8

Cannot we ever do better?

In some cases, we should. . . 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-9
SLIDE 9

Let us do better!

1 1 4 3 2 2 3 4 5 2

1 Chunk your data in non-decreasing runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-10
SLIDE 10

Let us do better!

4 runs of lengths 4, 1, 6 and 1 1 1 4 3 2 2 3 4 5 2

1 Chunk your data in non-decreasing runs 2 New parameters: Number of runs (ρ) and their lengths (r1, . . . , rρ)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-11
SLIDE 11

Let us do better!

4 runs of lengths 4, 1, 6 and 1 1 1 4 3 2 2 3 4 5 2

1 Chunk your data in non-decreasing runs 2 New parameters: Number of runs (ρ) and their lengths (r1, . . . , rρ)

New parameters: Run-length entropy: H = ρ

k=1(ri/n) log2(n/ri)

New parameters: Run-length entropy: H log2(ρ) log2(n)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-12
SLIDE 12

Let us do better!

4 runs of lengths 4, 1, 6 and 1 1 1 4 3 2 2 3 4 5 2

1 Chunk your data in non-decreasing runs 2 New parameters: Number of runs (ρ) and their lengths (r1, . . . , rρ)

New parameters: Run-length entropy: H = ρ

k=1(ri/n) log2(n/ri)

New parameters: Run-length entropy: H log2(ρ) log2(n)

Theorem [2,5]

Some stable algorithm has a worst-case time complexity of O(n + n H)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-13
SLIDE 13

Let us do better!

4 runs of lengths 4, 1, 6 and 1 1 1 4 3 2 2 3 4 5 2

1 Chunk your data in non-decreasing runs 2 New parameters: Number of runs (ρ) and their lengths (r1, . . . , rρ)

New parameters: Run-length entropy: H = ρ

k=1(ri/n) log2(n/ri)

New parameters: Run-length entropy: H log2(ρ) log2(n)

Theorem [2,5]

TimSort has a worst-case time complexity of O(n + n H)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-14
SLIDE 14

Let us do better!

4 runs of lengths 4, 1, 6 and 1 1 1 4 3 2 2 3 4 5 2

1 Chunk your data in non-decreasing runs 2 New parameters: Number of runs (ρ) and their lengths (r1, . . . , rρ)

New parameters: Run-length entropy: H = ρ

k=1(ri/n) log2(n/ri)

New parameters: Run-length entropy: H log2(ρ) log2(n)

Theorem [2,5]

TimSort has a worst-case time complexity of O(n + n H) We cannot do better than Ω(n + n H)![2] Reading the whole input requires a time Ω(n) There are X possible reorderings, with X 21−ρ

n r1 ... rρ

  • 2n H/2
  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-15
SLIDE 15

Contents

1

Efficient Merge Sorts

2

TimSort

3

Adaptive ShiversSort

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-16
SLIDE 16

A brief history of TimSort

2001 ’02 ’03 ’04 ’05 ’06 ’07 ’08 ’09 ’10 ’11 ’12 ’13 ’14 ’15 ’16 ’17 ’18 ’19

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-17
SLIDE 17

A brief history of TimSort

2001 ’02 ’03 ’04 ’05 ’06 ’07 ’08 ’09 ’10 ’11 ’12 ’13 ’14 ’15 ’16 ’17 ’18 ’19

1 1 Invented by Tim Peters[1]

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-18
SLIDE 18

A brief history of TimSort

2001 ’02 ’03 ’04 ’05 ’06 ’07 ’08 ’09 ’10 ’11 ’12 ’13 ’14 ’15 ’16 ’17 ’18 ’19

1 2

P

1 Invented by Tim Peters[1] 2 Standard algorithm in Python

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-19
SLIDE 19

A brief history of TimSort

2001 ’02 ’03 ’04 ’05 ’06 ’07 ’08 ’09 ’10 ’11 ’12 ’13 ’14 ’15 ’16 ’17 ’18 ’19

1 2

P

3 3 3

A J O

1 Invented by Tim Peters[1] 2 Standard algorithm in Python 3 Standard algorithm

———————— for non-primitive arrays in Android, Java, Octave

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-20
SLIDE 20

A brief history of TimSort

2001 ’02 ’03 ’04 ’05 ’06 ’07 ’08 ’09 ’10 ’11 ’12 ’13 ’14 ’15 ’16 ’17 ’18 ’19

1 2

P

3 3 3

A J O

4 1 Invented by Tim Peters[1] 2 Standard algorithm in Python 3 Standard algorithm

———————— for non-primitive arrays in Android, Java, Octave

4 1st worst-case complexity analysis[4] – TimSort works in time O(n log n)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-21
SLIDE 21

A brief history of TimSort

2001 ’02 ’03 ’04 ’05 ’06 ’07 ’08 ’09 ’10 ’11 ’12 ’13 ’14 ’15 ’16 ’17 ’18 ’19

1 2

P

3 3 3

A J O

4 5 1 Invented by Tim Peters[1] 2 Standard algorithm in Python 3 Standard algorithm

———————— for non-primitive arrays in Android, Java, Octave

4 1st worst-case complexity analysis[4] – TimSort works in time O(n log n) 5 Refined worst-case analysis[5] – TimSort works in time O(n + n H)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-22
SLIDE 22

A brief history of TimSort

2001 ’02 ’03 ’04 ’05 ’06 ’07 ’08 ’09 ’10 ’11 ’12 ’13 ’14 ’15 ’16 ’17 ’18 ’19

1 2

P

3 3 3

A J O

4 5 1 Invented by Tim Peters[1] 2 Standard algorithm in Python 3 Standard algorithm

———————— for non-primitive arrays in Android, Java, Octave

4 1st worst-case complexity analysis[4] – TimSort works in time O(n log n) 5 Refined worst-case analysis[5] – TimSort works in time O(n + n H)

Bugs uncovered in Python & Java implementations[3,5]

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-23
SLIDE 23

The principles of TimSort and of adaptive ShiversSort (1/2)

Algorithm based on merging adjacent runs 1 1 4 3 1 1 3 4

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-24
SLIDE 24

The principles of TimSort and of adaptive ShiversSort (1/2)

Algorithm based on merging adjacent runs 1 1 4 3 1 1 3 4 k ℓ

1 Run merging algorithm: standard + many optimizations ◮ time O(k + ℓ) ◮ memory O(min(k, ℓ))

  • Merge cost: k + ℓ
  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-25
SLIDE 25

The principles of TimSort and of adaptive ShiversSort (1/2)

Algorithm based on merging adjacent runs 1 1 4 3 1 1 3 4 k ℓ ≡ ≡ 4 1 5

1 Run merging algorithm: standard + many optimizations ◮ time O(k + ℓ) ◮ memory O(min(k, ℓ))

  • Merge cost: k + ℓ

2 Policy for choosing runs to merge: ◮ depends on run lengths only

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-26
SLIDE 26

The principles of TimSort and of adaptive ShiversSort (1/2)

Algorithm based on merging adjacent runs 1 1 4 3 1 1 3 4 k ℓ ≡ ≡ 4 1 5

1 Run merging algorithm: standard + many optimizations ◮ time O(k + ℓ) ◮ memory O(min(k, ℓ))

  • Merge cost: k + ℓ

2 Policy for choosing runs to merge: ◮ depends on run lengths only 3 Complexity analysis:

☛ Evaluate the total merge cost ☛ Forget array values and only work with run lengths

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-27
SLIDE 27

Some results about merge costs

Best-case merge costs: Every algorithm has a best-case merge cost of at least n H[2,8+] Worst-case merge costs: Every algorithm has a worst-case merge cost of at least n H + 2n[8] merge cost n H n H + 2n

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-28
SLIDE 28

Some results about merge costs

Best-case merge costs: Every algorithm has a best-case merge cost of at least n H[2,8+] Worst-case merge costs: Every algorithm has a worst-case merge cost of at least n H + 2n[8] TimSort has a worst-case merge cost of 3/2 n H + O(n)[5+,7] TimSort TimSort merge cost n H n H + 2n 3/2 n H

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-29
SLIDE 29

Some results about merge costs

Best-case merge costs: Every algorithm has a best-case merge cost of at least n H[2,8+] Worst-case merge costs: Every algorithm has a worst-case merge cost of at least n H + 2n[8] TimSort has a worst-case merge cost of 3/2 n H + O(n)[5+,7] PowerSort has a worst-case merge cost of n H + 2n[6]

but its merge policy is a bit complicated

PowerSort TimSort TimSort PowerSort merge cost n H n H + 2n 3/2 n H

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-30
SLIDE 30

Some results about merge costs

Best-case merge costs: Every algorithm has a best-case merge cost of at least n H[2,8+] Worst-case merge costs: Every algorithm has a worst-case merge cost of at least n H + 2n[8] TimSort has a worst-case merge cost of 3/2 n H + O(n)[5+,7] PowerSort has a worst-case merge cost of n H + 2n[6]

but its merge policy is a bit complicated

Adaptive ShiversSort has a worst-case merge cost of n H + ∆ n[8]

where ∆ = 24/5 − log2(5) ≈ 2.478 and its merge policy is simple

PowerSort TimSort TimSort Adaptive ShiversSort PowerSort merge cost n H n H + 2n n H + ∆ n 3/2 n H

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-31
SLIDE 31

Contents

1

Efficient Merge Sorts

2

TimSort

3

Adaptive ShiversSort

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-32
SLIDE 32

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

STACK

Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-33
SLIDE 33

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs: STACK

Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-34
SLIDE 34

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs: STACK

4 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-35
SLIDE 35

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs: STACK

4 1 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-36
SLIDE 36

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs: STACK

4 1 6 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-37
SLIDE 37

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs: STACK

4 1 6 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-38
SLIDE 38

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs:

1 1 3 4 2 2 3 4 5 2 ≡ 5 6 1

STACK

6 5 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-39
SLIDE 39

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs:

1 1 3 4 2 2 3 4 5 2 ≡ 5 6 1

STACK

5 6 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-40
SLIDE 40

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs:

1 1 3 4 2 2 3 4 5 2 ≡ 5 6 1 1 1 2 2 3 3 4 4 5 2 ≡ 11 1

STACK

11 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-41
SLIDE 41

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs:

1 1 3 4 2 2 3 4 5 2 ≡ 5 6 1 1 1 2 2 3 3 4 4 5 2 ≡ 11 1

STACK

11 1 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-42
SLIDE 42

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs:

1 1 3 4 2 2 3 4 5 2 ≡ 5 6 1 1 1 2 2 3 3 4 4 5 2 ≡ 11 1

STACK

11 1 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-43
SLIDE 43

The principles of adaptive ShiversSort and of TimSort (2/2)

1 1 4 3 2 2 3 4 5 2 ≡ 4 1 6 1

Discovered runs:

1 1 3 4 2 2 3 4 5 2 ≡ 5 6 1 1 1 2 2 3 3 4 4 5 2 ≡ 11 1 1 1 2 2 2 3 3 4 4 5 ≡ 12

STACK

12 Run merge policy: Maintain a stack of runs Until the array is sorted, either:

1

discover & push a new run length onto the stack

2

merge the top 1st and 2nd runs

3

merge the top 2nd and 3nd runs

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-44
SLIDE 44

Intermezzo: Intelligent design & amortized analysis

Key ideas:

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-45
SLIDE 45

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays its share of the total merge cost

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-46
SLIDE 46

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase)

Rule

3

STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-47
SLIDE 47

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase)

Rule

3

Run entry collapse STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-48
SLIDE 48

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase)

Merged run

STACK

r1 r2 r3 . . . rh−2 rh−1 rh Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-49
SLIDE 49

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase)

Managing the run entry phase: ensure that

◮ r pays for every merge ◮ (ri)i1 has exponential decay when r is pushed ◮ runs smaller than r are merged

Run entry collapse STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-50
SLIDE 50

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase) ◮ r to double its size (stabilisation phase)

Managing the run entry phase: ensure that

◮ r pays for every merge ◮ (ri)i1 has exponential decay when r is pushed ◮ runs smaller than r are merged

Run entry collapse STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-51
SLIDE 51

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase) ◮ r to increase its bit length (stabilisation phase)

r to increase its(bit length of r: ℓ = ⌊log2(r)⌋)

Managing the run entry phase: ensure that

◮ r pays for every merge ◮ (ri)i1 has exponential decay when r is pushed ◮ runs with smaller bit length than r are merged

Run entry collapse STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-52
SLIDE 52

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase) ◮ r to increase its bit length (stabilisation phase)

r to increase its(bit length of r: ℓ = ⌊log2(r)⌋)

Managing the run entry phase: ensure that

◮ r pays for every merge ◮ (ri)i1 has exponential decay when r is pushed ◮ runs with smaller bit length than r are merged

Managing the stabilisation phase: ensure that

◮ ri and ri+1 are merged only if their bit lengths are equal

Run entry collapse STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-53
SLIDE 53

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase) ◮ r to increase its bit length (stabilisation phase)

r to increase its(bit length of r: ℓ = ⌊log2(r)⌋)

Managing the run entry phase: ensure that

◮ r pays for every merge ◮ (ri)i1 has exponential decay when r is pushed ◮ runs with smaller bit length than r are merged

Managing the stabilisation phase: ensure that

◮ ri and ri+1 are merged only if their bit lengths are equal

Cost analysis: Each run r pays

☛ O(r) during its own run entry phase ☛ at most r⌈log2(n/r)⌉ during the stabilisation phases

Total merge cost of n H + O(n)

Run entry collapse STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-54
SLIDE 54

Intermezzo: Intelligent design & amortized analysis

Key ideas: Each run r pays

◮ O(r) to enter the stack (run entry phase) ◮ r to increase its bit length (stabilisation phase)

r to increase its(bit length of r: ℓ = ⌊log2(r)⌋)

Managing the run entry phase: ensure that

◮ r pays for every merge ◮ (ri)i1 has exponential decay when r is pushed ◮ runs with smaller bit length than r are merged

Managing the stabilisation phase: ensure that

◮ ri and ri+1 are merged only if their bit lengths are equal

Cost analysis: Each run r pays

☛ O(r) during its own run entry phase ☛ at most r⌈log2(n/r)⌉ during the stabilisation phases

Total merge cost of n H + O(n)

Run entry collapse STACK

r1 r2 r3 . . . rh−2 rh−1 . . . rk r Pushed run

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-55
SLIDE 55

The details of Adaptive ShiversSort

Choice rules for options

1 discover & push a new run length onto the stack 2 merge the top 1st and 2nd runs 3 merge the top 2nd and 3nd runs

STACK

. . . rh−2 rh−1 rh

2 3 1

Choice algorithm

if ℓh ℓh−2 or ℓh−1 ℓh−2: choose ③ else if ℓh ℓh−1: choose ② else: choose ① (or ② if ① is unavailable) where ℓi = ⌊log2(ri)⌋

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-56
SLIDE 56

The details of Adaptive ShiversSort

Choice rules for options

1 discover & push a new run length onto the stack 2 merge the top 1st and 2nd runs 3 merge the top 2nd and 3nd runs

STACK

. . . rh−2 rh−1 rh

2 3 1

Choice algorithm

if ℓh ℓh−2 or ℓh−1 ℓh−2: choose ③ else if ℓh ℓh−1: choose ② else: choose ① (or ② if ① is unavailable) where ℓi = ⌊log2(ri)⌋

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-57
SLIDE 57

The details of Adaptive ShiversSort

Choice rules for options

1 discover & push a new run length onto the stack 2 merge the top 1st and 2nd runs 3 merge the top 2nd and 3nd runs

STACK

. . . rh−2 rh−1 rh

2 3 1

Choice algorithm

if ℓh ℓh−2 or ℓh−1 ℓh−2: choose ③ else if ℓh ℓh−1: choose ② else: choose ① (or ② if ① is unavailable) where ℓi = ⌊log2(ri)⌋ Bit-length constraints: ℓ1 > ℓ2 > . . . > ℓh−2 ℓh−1 (induction) ℓ1 > ℓ2 > . . . > ℓh on run push ℓh−1 ℓh and ℓh−2 > ℓh during stabilisation (induction)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-58
SLIDE 58

The details of Adaptive ShiversSort

Choice rules for options

1 discover & push a new run length onto the stack 2 merge the top 1st and 2nd runs 3 merge the top 2nd and 3nd runs

STACK

. . . rh−2 rh−1 rh

2 3 1

Choice algorithm

if ℓh ℓh−2 or ℓh−1 ℓh−2: choose ③ else if ℓh ℓh−1: choose ② else: choose ① (or ② if ① is unavailable) where ℓi = ⌊log2(ri)⌋ Bit-length constraints: ℓ1 > ℓ2 > . . . > ℓh−2 ℓh−1 (induction)

exponential decay

ℓ1 > ℓ2 > . . . > ℓh on run push ℓh−1 ℓh and ℓh−2 > ℓh during stabilisation (induction)

merge ⇒ same bit-length

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-59
SLIDE 59

The details of Adaptive ShiversSort

Choice rules for options

1 discover & push a new run length onto the stack 2 merge the top 1st and 2nd runs 3 merge the top 2nd and 3nd runs

STACK

. . . rh−2 rh−1 rh

2 3 1

Choice algorithm

if ℓh ℓh−2 or ℓh−1 ℓh−2: choose ③ else if ℓh ℓh−1: choose ② else: choose ① (or ② if ① is unavailable) where ℓi = ⌊log2(ri)⌋ Bit-length constraints: ℓ1 > ℓ2 > . . . > ℓh−2 ℓh−1 (induction) ℓ1 > ℓ2 > . . . > ℓh on run push ℓh−1 ℓh and ℓh−2 > ℓh during stabilisation (induction) E N D O F P R O O F !

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-60
SLIDE 60

Conclusion

TimSort is good in practice and in theory: O(n + n H) merge cost

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-61
SLIDE 61

Conclusion

TimSort is good in practice and in theory: O(n + n H) merge cost PowerSort performs even better: n H + 2n merge cost

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-62
SLIDE 62

Conclusion

TimSort is good in practice and in theory: O(n + n H) merge cost PowerSort performs even better: n H + 2n merge cost Adaptive ShiversSort is both better and as simple as TimSort

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-63
SLIDE 63

Conclusion

TimSort is good in practice and in theory: O(n + n H) merge cost PowerSort performs even better: n H + 2n merge cost Adaptive ShiversSort is both better and as simple as TimSort Some references:

[1] Tim Peters’ description of TimSort, svn.python.org/projects/python/trunk/Objects/listsort.txt (2001) [2] On compressing permutations and adaptive sorting, Barbay & Navarro (2013) [3] OpenJDK’s java.utils.Collection.sort() is broken, de Gouw et al. (2015) [4] Merge strategies: from merge sort to TimSort, Auger et al. (2015) [5] On the worst-case complexity of TimSort, Auger et al. (2018) [6] Nearly-optimal mergesorts, Munro & Wild (2018) [7] Strategies for stable merge sorting, Buss & Knop (2019) [8] Adaptive ShiversSort: an alternative sorting algorithm, Jugé (2019)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm

slide-64
SLIDE 64

Conclusion

TimSort is good in practice and in theory: O(n + n H) merge cost PowerSort performs even better: n H + 2n merge cost Adaptive ShiversSort is both better and as simple as TimSort Some references:

[1] Tim Peters’ description of TimSort, svn.python.org/projects/python/trunk/Objects/listsort.txt (2001) [2] On compressing permutations and adaptive sorting, Barbay & Navarro (2013) [3] OpenJDK’s java.utils.Collection.sort() is broken, de Gouw et al. (2015) [4] Merge strategies: from merge sort to TimSort, Auger et al. (2015) [5] On the worst-case complexity of TimSort, Auger et al. (2018) [6] Nearly-optimal mergesorts, Munro & Wild (2018) [7] Strategies for stable merge sorting, Buss & Knop (2019) [8] Adaptive ShiversSort: an alternative sorting algorithm, Jugé (2019)

  • V. Jugé

Complexity of the Adaptive ShiversSort Algorithm