1.51k likes | 1.68k Views
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
E N D
In-Place Sorting Viliam Geffert P.J.Šafárik University, Košice, Slovakia Jozef Gajdoš College of International Business ISM Slovakia Prešov, Slovakia The Problem
n elements 1hole 1.n.log n + Δ(n) comparisons O(n)element moves O(1) index variables, of log nbits each
Comparisons Moves Storage log n! ≈ n.log n–1.44n 3/2.n O(1) lower bound
Comparisons Moves Storage log n! ≈ n.log n–1.44n 3/2.n O(1) lower bound n.log n n.log n n mergesort [Kn1973]
Comparisons Moves Storage 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]
Comparisons Moves Storage 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
Comparisons Moves Storage 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]
Comparisons Moves Storage 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
Comparisons Moves Storage 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]
Comparisons Moves Storage 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
Comparisons Moves Storage 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+ε).n O(1) [GeFr2005]
Comparisons Moves Storage 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+ε).n O(1) [GeFr2005] log n! +Θ(n.loglog n) (26+ε).n O(1) our result
2n/log2 n largest elements Modified heapsort -- branching degree log n instead of 2
2n/log2 n largest elements 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 smallest elements 2n/log2 n largest elements Com: O(n) Mov : O(n/log n)
2n/log2 n smallest elements 2n/log2 n largest elements Symmetrically, by modified heapsort Com: O(n) Mov : O(n/log n)
2n/log2 n smallest elements 2n/log2 n largest elements X Y Com: 1 Mov: -- Com: 1 Mov: --
2n/log2 n smallest elements 2n/log2 n largest elements X Y If X=Y, we are done, the array is sorted
2n/log2 n smallest elements 2n/log2 n largest elements X Y If X<Y, continue
2n/log2 n smallest elements 2n/log2 n largest elements Pointer memory
2n/log2 n smallest elements 2n/log2 n largest elements 0 0 0 0 0 0 Pointer memory
2n/log2 n smallest elements 2n/log2 n largest elements 1 0 0 0 0 0 Pointer memory
2n/log2 n smallest elements 2n/log2 n largest elements 1 0 1 0 0 0 Pointer memory
2n/log2 n smallest elements 2n/log2 n largest elements 1 1 1 1 1 1 2n/log2 n element pairs ≡ 2n/log3 ninteger variables, oflog nbits each
ni elements an element of rank ¼.ni b L R
ni elements an element of rank ¼.ni b L R
ni elements an element of rank ¼.ni b L R Com1: O( ni ) Mov1 : . ni
ni elements an element of rank ¼.ni b L R
ni elements an element of rank ¼.ni b L R A< to be sorted B≥ buffer memory
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< to be sorted B≥ buffer memory
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< to be sorted B≥ buffer memory Com1: O( ni ) Mov1 : 2.ni,<
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< to be sorted B≥ buffer memory Using pointer memory L+R and buffer memory B≥ , sort A<
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< to be sorted B≥ buffer memory Using pointer memory L+R and buffer memory B≥ , sort A<
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R Pointer memory ≥ 2*2m/log2 m Buffer memory ≥ 3m A< B≥ Com?: m.log m + O(m.loglog m) Mov?: (24+).m Using pointer memory L+R and buffer memory B≥ , sort A<
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< B≥ Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< B≥ Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< B≥ Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni ni,≥≥ 3.ni,< ni,<≤ ¼.ni b L R A< B≥ Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni ni,>≤ ¾.ni ni,<≤ ¼.ni b L R A< B≥ Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni ni,>≤ ¾.ni ni,<≤¼ni,= b L R A< B≥ Com1: ni Mov1: 3.ni,= Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni ni,>≤ ¾.ni ni,<≤¼ni,= L R sorted Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni ni+1≤ ¾.ni “new” ni ni,<≤¼ ni,= L R sorted Com?: m.log m + O(m.loglog m) Mov?: (24+).m
an element of rank ¼.ni+1 ni+1≤ ¾.ni ni,<≤¼ ni,= L R sorted Com?: m.log m + O(m.loglog m) Mov?: (24+).m