In-Place Sorting Viliam Geffert P.J. Š af á rik University , K ošice, Slovakia J o zef Gajdo š

151
In-Place Sorting In-Place Sorting Viliam Geffert Viliam Geffert P.J. P.J. Š Š af af á á rik University rik University , , K K ošice, Slovakia ošice, Slovakia J J o o zef Gajdo zef Gajdo š š College of International Business ISM College of International Business ISM Slovakia Slovakia Prešov Prešov , Slovakia , Slovakia The Problem

description

In-Place Sorting Viliam Geffert P.J. Š af á rik University , K ošice, Slovakia J o zef Gajdo š College of International Business ISM Slovakia Prešov , Slovakia. The Problem. n elements. n elements. n elements. 1 hole. 1. n.log n + Δ (n) comparisons O(n) element moves - PowerPoint PPT Presentation

Transcript of In-Place Sorting Viliam Geffert P.J. Š af á rik University , K ošice, Slovakia J o zef Gajdo š

Page 1: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

In-Place SortingIn-Place Sorting

Viliam GeffertViliam GeffertP.J.P.J.ŠŠafafáárik Universityrik University, , KKošice, Slovakiaošice, Slovakia

JJoozef Gajdozef Gajdošš College of International Business ISM College of International Business ISM

SlovakiaSlovakiaPrešovPrešov, Slovakia, Slovakia

The Problem

Page 2: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

n elements

Page 3: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

n elements

Page 4: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

n elements

1.n.log n + Δ(n) comparisons

O(n) element moves

O(1) index variables, of log n bits each

1 hole

Page 5: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

Page 6: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n n.log n nmergesort

[Kn1973]

Page 7: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Page 8: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Θ(n2) O(n)O(1) selectsort

Page 9: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Θ(n1+) O(n)O(1) n1/k-heapsort

[MuRa1992]

Page 10: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Θ(n1+) O(n)O(1) n1/k-heapsort

[MuRa1992]

n.log n O(n)Θ(n) tablesort

Page 11: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Θ(n1+) O(n)O(1) n1/k-heapsort

[MuRa1992]

Θ(n.log n) O(n)Θ(n) samplesortADV

[MuRa1992]

Page 12: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Θ(n1+) O(n)O(1) n1/k-heapsort

[MuRa1992]

Θ(n.log n) O(n)Θ(n) samplesortADV

[MuRa1992]

1991-2005: conjectured that an algorithm matching

the lower bounds for all criteria does not exist

Page 13: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Θ(n1+) O(n)O(1) n1/k-heapsort

[MuRa1992]

Θ(n.log n) O(n)Θ(n) samplesortADV

[MuRa1992]

log n! +Θ(n.log n) (13+ε).nO(1) [GeFr2005]

Page 14: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Comparisons MovesStorage

log n! ≈ n.log n–1.44n 3/2.n

O(1) lower bound

n.log n +O(n.loglog n) O(n.log n/loglog n)O(1)mergesortADV

[KaPa1999]

Θ(n1+) O(n)O(1) n1/k-heapsort

[MuRa1992]

Θ(n.log n) O(n)Θ(n) samplesortADV

[MuRa1992]

log n! +Θ(n.log n) (13+ε).nO(1) [GeFr2005]

log n! +Θ(n.loglog n) (26+ε).nO(1) our result

Page 15: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

1. Initial Preparation

Page 16: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š
Page 17: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

2n/log2 n largest elements

Page 18: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

2n/log2 n largest elements

Page 19: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Modified heapsort

-- branching degree log n instead of 2

2n/log2 n largest elements

Page 20: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Modified heapsort

-- branching degree log n instead of 2-- halts after extracting 2n/log2 n largest elements

Com: O(n)Mov : O(n/log n)

2n/log2 n largest elements

Page 21: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Com: O(n)Mov : O(n/log n)

2n/log2 n smallest elements 2n/log2 n largest elements

Page 22: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Symmetrically, by modified heapsort

Com: O(n)Mov : O(n/log n)

2n/log2 n smallest elements 2n/log2 n largest elements

Page 23: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

X Y

2n/log2 n smallest elements 2n/log2 n largest elements

Com: 1Mov: --Com: 1Mov: --

Page 24: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

If X=Y, we are done, the array is sorted

2n/log2 n smallest elements 2n/log2 n largest elements

X Y

Page 25: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

If X<Y, continue

2n/log2 n smallest elements 2n/log2 n largest elements

X Y

Page 26: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Pointer memory

2n/log2 n smallest elements 2n/log2 n largest elements

Page 27: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

0

0

0

0

0

0

Pointer memory

2n/log2 n smallest elements 2n/log2 n largest elements

Page 28: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

1

0

0

0

0

0

Pointer memory

2n/log2 n smallest elements 2n/log2 n largest elements

Page 29: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

1

0

1

0

0

0

Pointer memory

2n/log2 n smallest elements 2n/log2 n largest elements

Page 30: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

1

1

1

1

1

1

2n/log2 n smallest elements 2n/log2 n largest elements

2n/log2 n element pairs ≡

2n/log3 n integer variables, of log n bits each

Page 31: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

2. Partition-Based Sorting

Page 32: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bL R

ni elementsan element of rank ¼.ni

Page 33: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

ni elements

b

an element of rank ¼.ni

Page 34: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

ni elements

b

Com1: O( ni )Mov1 : . ni

an element of rank ¼.ni

Page 35: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

ni elements

b

an element of rank ¼.ni

Page 36: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

ni elements

b

A<

to be sortedB≥

buffer memory

an element of rank ¼.ni

Page 37: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L Rb

A<

to be sortedB≥

buffer memory

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

an element of rank ¼.ni

Page 38: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

A<

to be sortedB≥

buffer memory

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

Com1: O( ni )Mov1 : 2.ni,<

Page 39: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

A<

to be sortedB≥

buffer memory

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

Using pointer memory L+R and buffer memory B≥ ,sort A<

Page 40: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

A<

to be sortedB≥

buffer memory

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

Using pointer memory L+R and buffer memory B≥ ,sort A<

Page 41: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

A< B≥

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

Using pointer memory L+R and buffer memory B≥ ,sort A<

Pointer memory ≥ 2*2m/log2 mBuffer memory ≥ 3m

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 42: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

B≥A<

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 43: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

B≥A<

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 44: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

B≥A<

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 45: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

ni,< ≤ ¼.ni

ni,≥ ≥ 3.ni,<

B≥A<

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 46: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

ni,< ≤ ¼.ni

ni,> ≤ ¾.ni

B≥A<

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 47: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

an element of rank ¼.ni

b

B≥A<Com1:ni

Mov1:

3.ni,=

ni,> ≤ ¾.ni

ni,<≤¼ ni,=

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 48: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni

ni,> ≤ ¾.ni

ni,<≤¼ ni,=

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 49: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

“new” ni

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 50: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 51: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 52: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

A< B≥

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 53: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

A< B≥

sort

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 54: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

A< B≥

sort

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 55: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

A< B≥

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 56: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

A< B≥

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 57: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,=

A< B≥

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 58: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+1 ≤ ¾.ni

ni,<≤¼ ni,= ni+1,< ni+1,=

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 59: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+2 ≤ ¾.ni+1

ni,<≤¼ ni,= ni+1,< ni+1,=

“new” ni

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 60: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

sortedL R

an element of rank ¼.ni+1

ni+2 ≤ ¾.ni+1

ni,<≤¼ ni,= ni+1,< ni+1,=

… … …

iterate

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 61: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R… … …

iterate

ni+1 ≤ ¾.ni

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 62: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

ni+1 ≤ ¾.ni

restore order

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 63: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L R

ni+1 ≤ ¾.ni

restore order

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 64: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

L RCom: O(n/log2 n)Mov: O(n/log2 n)

ni+1 ≤ ¾.ni

restore order

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 65: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 66: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 67: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 68: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)Processing one block, of length ni: O(ni) + .ni +

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 69: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)Processing one block, of length ni: O(ni) + .ni + ni,<

.log n + ni,<.O(loglog n) (26+).ni,< +

26.ni,=

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 70: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)Processing one block, of length ni: O(ni) + .ni + ni,<

.log n + ni,<.O(loglog n) (26+).ni,< +

26.ni,=

Block lengths: n1,< + n1,= + n2,< + n2,= + … ≤ O(n) ⇐ ni+1 ≤ ¾.ni

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 71: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)Processing one block, of length ni: O(ni) + .ni + ni,<

.log n + ni,<.O(loglog n) (26+).ni,< +

26.ni,=

Block lengths: n1,< + n1,= + n2,< + n2,= + … ≤ O(n) ⇐ ni+1 ≤ ¾.ni n1,< + n1,= + n2,< + n2,= + … ≤ n ⇐ disjoint Com?: m.log

m+ O(m.loglog

m)Mov?: (24+).m

Page 72: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)Processing one block, of length ni: O(ni) + .ni + ni,<

.log n + ni,<.O(loglog n) (26+).ni,< +

26.ni,=

Block lengths: n1,< + n1,= + n2,< + n2,= + … ≤ O(n) n1,< + n1,= + n2,< + n2,= + … ≤ n

Total comparisons: n.log n + n.O(loglog n) + O(n)

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 73: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)Processing one block, of length ni: O(ni) + .ni + ni,<

.log n + ni,<.O(loglog n) (26+).ni,< +

26.ni,=

Block lengths: n1,< + n1,= + n2,< + n2,= + … ≤ O(n) n1,< + n1,= + n2,< + n2,= + … ≤ n

Total comparisons: n.log n + n.O(loglog n)

Total moves: (26+).n

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 74: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Total comparisons: Total moves:

Creating/clearing pointer memory: O(n) O(n/log n)Processing one block, of length ni: O(ni) + .ni + ni,<

.log n + ni,<.O(loglog n) (26+).ni,< +

26.ni,=

Block lengths: n1,< + n1,= + n2,< + n2,= + … ≤ O(n) n1,< + n1,= + n2,< + n2,= + … ≤ nProvided that:

Total comparisons: n.log n + n.O(loglog n)

Total moves: (26+).n

Com?: m.log m

+ O(m.loglog m)Mov?: (24+).m

Page 75: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

3.Sorting A with AuxiliaryBuffer and Pointer Memories

Basic Idea

Page 76: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

B

Page 77: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

B

Page 78: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

evenly distributed frame of sizem/log4 m … 2m/log4 m

Page 79: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

a2 ≤ a1

m/log4 m … 2m/log4 m

Page 80: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

a1 … a2

m/log4 m … 2m/log4 m

Page 81: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

a2 … a3

m/log4 m … 2m/log4 m

Page 82: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

a3 … a4

m/log4 m … 2m/log4 m

Page 83: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

af ≤ a4

m/log4 m … 2m/log4 m

Page 84: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

frame growsdynamically

m/log4 m … 2m/log4 m

Page 85: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

frame growsdynamically

median

m/log4 m … 2m/log4 m

Page 86: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

frame growsdynamically

≤ ≥

m/log4 m … 2m/log4 m

Page 87: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

frame growsdynamically

≤ ≥

m/log4 m … 2m/log4 m

Page 88: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

≤ ≥

m/log4 m … 2m/log4 m

Page 89: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

m/log4 m … 2m/log4 m

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

segments sorted by[Geffert & Franceschini

2005]

Page 90: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

m/log4 m … 2m/log4 m

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

segments sorted by[Geffert & Franceschini

2005]

Page 91: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

m/log4 m … 2m/log4 m

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

segments sorted by[Geffert & Franceschini

2005]

Page 92: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

m/log4 m … 2m/log4 m

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

segments sorted by[Geffert & Franceschini

2005]

Page 93: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

m/log4 m … 2m/log4 m

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

segments sorted by[Geffert & Franceschini

2005]

Page 94: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

m/log4 m … 2m/log4 m

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

segments sorted by[Geffert & Franceschini

2005]

Page 95: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

segments of size½.log4 m … log4 m

m/log4 m … 2m/log4 m

Page 96: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

a1

a2

a3

af

Ϭ0 Ϭ1 Ϭ2 Ϭ3 Ϭf

B

Page 97: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

A

B

Page 98: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Data Structure

Page 99: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

to be sortedA

melements

Page 100: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

to be sortedA

melements

:

pointer memory

2* 2m/log3 m* log m

Page 101: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

to be sortedA

melements

:

pointer memory

2* 2m/log3 m* log m

buffer memory B

size 3m

Page 102: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

m

:

pointer memory

2* 2m/log3 m* log m

3mto be sorted

A

Page 103: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

m

to be sortedA

Page 104: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

m

to be sortedA

Page 105: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

m

to be sortedA

Page 106: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

m

to be sortedA

Page 107: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

hole

m

to be sortedA

Page 108: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

m

to be sortedA

Page 109: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

m

to be sortedA

Page 110: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

m

to be sortedA

Page 111: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

m

to be sortedA

Page 112: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

m

to be sortedA

Page 113: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

m

to be sortedA

Page 114: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

:

pointer memory

2* 2m/log3 m* log m

3m

b -- smallest buffer element

Comparisons Comparisons

Moves 2 * Moves

3m free locations + 1 free location

+ 3m buffer elements

m

to be sortedA

b

Page 115: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

frameR

2m/log3m

≤ m/2

:

pointer memory

2* 2m/log3 m* log m

m

to be sortedA

b

Page 116: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

frameR

2m/log3m

≤ m/2

:

pointer memory

2* 2m/log3 m* log m

m

to be sortedA

b

to store only2m/log4 m elements

Page 117: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

frameR

2m/log3m

≤ m/2

cacheC

2m/log2m

≤ m/2

:

pointer memory

2* 2m/log3 m* log m

m

to be sortedA

b

Page 118: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

frameR

2m/log3m

≤ m/2

cacheC

2m/log2m

≤ m/2

segmentsS

≤ 2m/log4m * log4m

≤ 2m

:

pointer memory

2* 2m/log3 m* log m

m

to be sortedA

b

Page 119: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

to be sortedA

frameR

cacheC

segmentsS

R:

:

2m/log4 m * log m

b

Page 120: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

to be sortedA

frameR

cacheC

segmentsS

R:

:

2m/log4 m * log m

b

sorted frame

Page 121: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

to be sortedA

frameR

cacheC

segmentsS

R:

:

sorted frame

buffer elementsnot sorted,larger than active elements

2m/log4 m * log m

b

Page 122: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

to be sortedA

frameR

cacheC

segmentsS

R:

:

a

a

2m/log4 m * log m

b

Page 123: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

a

binary search with a:

log(2m/log4 m) ≤ log m

a

2m/log4 m * log m

b

Page 124: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

a

2m/log4 m * log m

b

Page 125: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

a

binary search with a:loglog m

a

2m/log4 m * log m

b

Page 126: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

C:

a

2m/log4 m * log m

2m/log3 m * log m

b

Page 127: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

ak … ak+1

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

C:k.log m

akak+1

a

2m/log3 m * log m

b

2m/log4 m * log m

Page 128: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

a

not sortedbuffer elements not sorted,larger than active elements

a2m/log3 m * log m

b

Page 129: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

a

b

a2m/log3 m * log m

b

Page 130: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

a

to be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

a

a

bbinary search with

b:loglog m

2m/log3 m * log m

b

Page 131: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

2m/log3 m * log mC:

akak+1

2m/log4 m * log m

S: … …

≤ 2m/log4 m * log4 m

k.log m

Page 132: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

k.log m

k.log m k.log m

≤ 2m/log4 m * log4 m

2m/log4 m * log m

Page 133: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

k.log m

k.log m k.log m

≤ 2m/log4 m * log4 m

Page 134: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

k.log m

k.log m

decoding segment address:

log m comparisons

≤ 2m/log4 m * log4 m

Page 135: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …ak … ak+1

≤ 2m/log4 m * log4 m

Page 136: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

≤ 2m/log4 m * log4 m

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

not sortednot sorted

Page 137: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

b

≤ 2m/log4 m * log4 m

Page 138: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

b

binary search with b:

log(log4 m) ≤ 4.loglog m

≤ 2m/log4 m * log4 m

Page 139: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

≤ 2m/log4 m * log4 m

2m/log3 m * log m

2m/log4 m * log m

Page 140: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

bto be sortedA

frameR

cacheC

segmentsS

R:

:

C:

akak+1

S: … …

≤ 2m/log4 m * log4 m

2m/log3 m * log m

2m/log4 m * log m

Com: m.log m +O(m.loglog m)Mov : O(m)

Page 141: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Final Remarks

Page 142: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result

Comparisons: log n! +Θ(n.loglog n)

Moves: (26+ε).n

Storage: O(1)

Page 143: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result[GeFr2005]

Comparisons: log n! +Θ(n.loglog n) log n! +Θ(n.log n)

Moves: (26+ε).n(13+ε).n

Storage: O(1) O(1)

Page 144: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result[GeFr2005]

Comparisons: log n! +Θ(n.loglog n) log n! +Θ(n.log n)

Moves: (26+ε).n(13+ε).n

Storage: O(1) O(1)

cache memory

short segments

sorted by [GeFr2005]

Page 145: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result

Comparisons: log n! +Θ(n.loglog n)

Moves: (26+ε).n

Storage: O(1)

- mainly of theoretical interest, too complex to implement

Page 146: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result

Comparisons: log n! +Θ(n.loglog n)

Moves: (26+ε).n

Storage: O(1)

- mainly of theoretical interest, too complex to implement- however, several new techniques and data structures can be utilized in other applications

Page 147: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result

Comparisons: log n! +Θ(n.loglog n)

Moves: (26+ε).n

Storage: O(1)

- mainly of theoretical interest, too complex to implement- however, several new techniques and data structures can be utilized in other applications

? probably, (26+ε).n moves can be substantially improved

[proceedings: (34+ε).n moves]

Page 148: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result

Comparisons: log n! +Θ(n.loglog n)

Moves: (26+ε).n

Storage: O(1)

- mainly of theoretical interest, too complex to implement- however, several new techniques and data structures can be utilized in other applications

? probably, (26+ε).n moves can be substantially improved

[proceedings: (34+ε).n moves]? log n! +O(n) comparisons

Page 149: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Our result

Comparisons: log n! +Θ(n.loglog n)

Moves: (26+ε).n

Storage: O(1)

- mainly of theoretical interest, too complex to implement- however, several new techniques and data structures can be utilized in other applications

? probably, (26+ε).n moves can be substantially improved

[proceedings: (34+ε).n moves]? log n! +O(n) comparisons? stable version (equal buffer elements are mixed up)

Page 150: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š

Thank You for Your Attention

Page 151: In-Place Sorting Viliam Geffert P.J. Š af á rik University ,  K ošice, Slovakia J o zef Gajdo š