Introduction to Quick Sort
EUnS
20191115
...................................... 1
1.................................... 3
2............................. 3
3.............................. 6
3.1 ............................. 6
3.2 하한 ..................... 6
3.3 ................................. 7
4.......................... 9
4.1 ........................ 9
4.2 ........................ 9
4.3 방법 .................. 10
5................................. 13
5.1 ........................ 13
5.2 ........................... 13
5.3 Hoare’s Partition VS Lomuto’s Partition . . . . . . . . . . . . 16
6 Quick sort........................ 19
7.................................... 21
7.1 ........................... 21
7.2 hybridsort............................. 21
7.3 ............................ 22
7.4 medianofthree .......................... 25
7.5 Parallelization........................... 25
1
8................................ 26
8.1 HOARE VS LOMUTO . . . . . . . . . . . . . . . . . . . . . 27
8.2 .......................... 27
8.3 std::sort VS J. Bentley D. McIlroy . . . . . . . . . 30
................................... 32
.................................... 32
................................... 33
33
2
1
:O(nlg n)
:O(n2)
C++ std::sort있음1
2
(divide and conquer) 방법.
5
.
1QUICKSORT(A, p , r )
2i f p<r
3q = PARTITION(A, p , r )
4QUICKSORT(A, p , q1)
5QUICKSORT(A, q+1, r )
Lomuto’s Partition Scheme
1PARTITION(A , p , r )
2x= A[ r ] // p i v o t
3i = p1
4f o r j = p to r1
5i f A[ j ]<= x
6i = i + 1
7exchang e A[ i ] with A[ j ]
8exchang e A[ i +1] with A[ r ]
9return i + 1
1introsort : quicksortheapsort, insertionsort.
3
Hoare, C. A. R.1961음으Quick sort. PARTITION
Lomuto(1999)PARTITION
, HoarePartition
.
PARTITION Θ(n).
4
1: quick sort [1]
5
3
3.1
3.1 (),하한,
big O O
f(n), g(n)0f(n)cg(n)(nn0)n0,
cf(n) = O(g(n)).
하한 omega
f(n), g(n)0cg(n)f(n)(nn0)n0,
cf(n) = Ω(g(n)).
Theta Θ
Θ(g(n))f(n) = O(g(n))f(n) = Ω(g(n))
.
3.2 하한
n
X
k=1
1
k= Θ(lg n)
f(k)음이
Zn
m1
f(x)dx
n
X
k=m
f(k)Zn+1
m
f(x)dx
f(k)음이 4 .
해할 .
Zn+1
m
f(x)dx
n
X
k=m
f(k)Zn
m1
f(x)dx
n
X
k=2
1
k+ 1 Zn+1
2
f(x)dx + 1 = ln(x) + 1 = O(ln x)
Zn+1
1
f(x)dx = ln(x+ 1) = Ω(ln x)
n
X
k=1
1
k
n
X
k=1
1
k= Θ(lg n)
6
2: [1]
3.3
Indicator random variables
I{A}=(1 ( H)
0 ( ¯
H)
7
E[XA] = E[I{A}]
= 1 ×Pr(A)+0×Pr( ¯
A)2
= Pr(A)
2PrA
8
4
quicksort
음을 .
4.1
(,).
..
T(n) = T(n1) + cn
T(n) = T(n1) + cn
=T(n2) + c(n1) + cn
=c
n
X
k=1
k
=1
2cn2
= Θ(n2)
Θ(n2).
4.2
.
T(n) = 2T(n
2) + cn
.
2 리를 Θ(nlg n)
.3
3marster theory.
9
3: quick sort [1]
4.3 방법
.
9:1할하
우와
9:1할하
음의 음이 .
T(n)T9n
10 +Tn
10+cn
10
4: 9:1할하[1]
.log10 n이의 cn
cn작은 log10/9ncn
작으nlog10/9n=O(nlog n).T(n) = Θ(nlog n)
우와
5: ,[1]
T(n) T(n-1)Θ(n).
11
Θ(n)
Θ(nlg n).
12
5
5.1
음이 .
T(n) = max
0qn1(T(q) + T(nq1)) + Θ(n)
T(n)Θ(n2)임을 .
.T(n)c1n2.
T(n)max
0qn1(c1q2+c1(nq1)2) + Θ(n)
.
0qn1c1q2+c1(nq1)2
f(q) = c1q2+c1(nq1)2f(q)=2c1q2c1(nq1)
q=n1
2.q
.q= 0
q=n1
.
T(n)c1(n1)2+ Θ(n)
c1n2c(2n1) + Θ(n)
c1n2
Θ(n) = dnc1> dc1결과
T(n)c1n2음을 .c2n2T(n)임을
c2< d잡음으T(n)c2n2음을 .
T(n) = Θ(n2).
5.2
1n.
13
E[X] = E"n
X
i=1
Xi#
=
n
X
i=1
E[Xi]
음의 조정리를 .
Lemma 5.1. Xn열에QUICKSORT
PARTITION4 QUICKSORT
O(N+X).
Proof. PARTITIONn .for
행하, for 반복4행한.
X
.
A={z1, z2, ..., zn}.
Zij ={zi, zi+1, ..., zj}.zizj.
이유PARTITIONpivot
,pivot는다.
Xij =I{zizj}
X=
n1
X
i=1
n
X
j=i+1
Xij
E[x] = E
n1
X
i=1
n
X
j=i+1
Xij
=
n1
X
i=1
n
X
j=i+1
E[Xij ]
=
n1
X
i=1
n
X
j=i+1
P r{zizj}
14
P r{zizj=P r{Zij zizj.}
=P r{Zij zi.}+P r{Zij zj.}
=1
ji+ 1 +1
ji+ 1
=2
ji+ 1
E[X] =
n1
X
i=1
n
X
j=i+1
2
ji+ 1
jik.
E[X] =
n1
X
i=1
n
X
j=i+1
2
ji+ 1
=
n1
X
i=1
ni
X
k=1
2
k+ 1
n1
X
i=1
n
X
k=1
2
k
n1
X
i=1
clg n
cn lg n
E[X] = O(nlg n)
15
하한 .
E[X] =
n1
X
i=1
ni
X
k=1
2
k+ 1
=
n1
X
k=1
2
k+ 1 +
n2
X
k=1
2
k+ 1 +· · · +
1
X
k=1
2
k+ 1
= (n1) 2
1+1+ (n2) 2
2+1+· · · + 1 ×2
(n1) + 1
=
n1
X
k=1
2
k+ 1 ×(nk)
=
n1
X
k=1 2n
k+ 1 2k
k+ 1
= 2n
n1
X
k=1
1
k+ 1 2
n1
X
k=1
k
k+ 1
2nc lg n2(n1)
n1
X
k=1
k
k+ 1
n1
X
k=1 k
k+ 1 +1
k+ 1!
Ω(nlg n)
Θ(nlg n)
5.3 Hoare’s Partition VS Lomuto’s Partition
Lomuto’s Partition Scheme
1PARTITION(A , p , r )
2x= A[ r ] // p i v o t
3i = p1
4f o r j = p to r1
5i f A[ j ]<= x
6i = i + 1
7exchang e A[ i ] with A[ j ]
8exchang e A[ i +1] with A[ r ]
9return i + 1
Hoare’s partition scheme
1PARTITION(A , p , r )
2x= A[ r ] // p i v o t
16
3i = p
4j = r
5while TRUE
6repeat
7j = j 1
8u n t i l A[ j ] <= x
9repeat
10 i = i + 1
11 u n t i l A[ i ] >= x
12 i f i<j
13 exchang e A[ i ] with A[ j ]
14 e l s e r e t u r n j
니다 stackexchange
해하 Lomuto’s Partition.
.
음을 .
n-1.
Lomuto’s Partition1<=x <=npivotx
x1행한.
1
n
n
X
x=1
(x1) = n1
2
Hoare’s Partitionij작은
행한.행하ij(
)결과4
(nx)(x1)
n1.
1
n
n
X
x=1
(nx)(x1)
n1=n2
6
4니다 ㅠㅠ
17
Hoare’s Partition행하는다 Lomuto’s Partitionn/2
행한.
있을
Hoare’s Partition. Lomuto’s Partition
행하i = n
Θ(n2).
Hoare’s Partition
.
정적
Lomuto’s Partition .5
음의 결과.6
5: hoare
6Hoare, Lomuto testing .
18
6 Quick sort
음은 Radix sort()Quick sort n
/n.O(n)
nQuick sort.
간과 ,
.부분
결과알아야.
:n5000
.
19
6: Comparing Quicksort and Radix Sort by (a) instructions executed per
item sorted (b) time per item sorted, and (c) cache misses per item sorted.
This data is from a paper by LaMarca and Ladner [1996]. Due to such results,
new versions of Radix Sort have been invented that take memory hierarchy
into account, to regain its algorithmic advantages. Th e basic idea of cache
optimizations is to use all the data in a block repeatedly before it is replaced
on a miss.[2]
20
7
7.1
loop제점음이 .
수스
메모
quick sort반복.
7.2 hybrid sort
Introsort
Quicksort
heapsort. heapsortQuicksortO(nlog n)
,악에.
2 log2n.
Quick insertion sort
(insertion sort)O(n2)
()O(n).()작은 n
nquick sort
.
.n 10작을
행하.
음은 행하n.
N= 10000 .
1INSERTION SORT(A)
2f o r j = 2 t o A. l e n g t h
3key = A[ j ]
4i = j 1
5while i>0 and A[ i ] >key
6A[ i +1] = A[ i ]
21
7: n[3]
7i = i 1
8A[ i +1] = key
7.3
(Dutch national flag problem)
quick sort.열에
.lgn
..3 way partitioning
.작은가고
.음은 Dijstra .c++.
22
8: 3-way-partitioning [4]
1vo id Quick3way ( i n t a [ ] , i n t l o , i n t h i )
2{
3i f ( h i <= l o )
4return ;
5i n t l t = lo , i = l o + 1 , gt = hi ;
6i n t v = a [ l o ] ;
7while ( i <= g t )
8{
9i f ( a [ i ] <v )
10 {
11 st d : : swap ( a [ l t ] , a [ i ] ) ;
12 l t ++, i ++;
13 }
14 e l s e i f ( a [ i ] >v )
15 {
16 st d : : swap ( a [ g t ] , a [ i ] ) ;
17 gt −−;
18 }
19 e l s e // a [ i ]==v
20 {
21 i ++;
22 }
23 }
24 Quick3way ( a , l o , l t 1) ;
23
25 Quick3way ( a , gt + 1 , hi ) ;
26 }
음은 J. BentleyD. McIlroy .
9: Fast 3-way partitioning [4]
1vo id q u i c k s o r t ( Item a [ ] , i n t l , i n t r )
2{
3i n t i = l 1, j = r , p = l 1, q = r ; Item v = a [ r ] ;
4i f ( r <= l ) return ;
5f o r ( ; ; )
6{
7while ( a[++ i ] <v ) ;
8while ( v <a[−− j ] )
9i f ( j == l ) break ;
10 i f ( i >= j )
11 break ;
12 exch ( a [ i ] , a [ j ] ) ;
13 i f ( a [ i ] == v )
14 {
15 p++;
16 exch ( a [ p ] , a [ i ] ) ;
17 }
18 i f ( v == a [ j ] )
19 {
20 q−−;
24
21 exch ( a [ j ] , a [ q ] ) ;
22 }
23 }
24 exch ( a [ i ] , a [ r ] ) ;
25 j = i 1;
26 i = i +1;
27 f o r ( k = l ; k <p ; k++, j )
28 exch ( a [ k ] , a [ j ] ) ;
29 f o r ( k = r 1; k >q ; k−−, i ++)
30 exch ( a [ i ] , a [ k ] ) ;
31 q u i c k s o r t ( a , l , j ) ;
32 q u i c k s o r t ( a , i , r ) ;
33 }
34
7.4 median of three
Sedgewick.위의 pivot.
p,r
수시,
간값방법.
7.5 Parallelization
각각메모
.
이인 O(lg n).
.
and ... 있으7. quicksort
(pivot).8
7음이 .Yukun Yao.(2019) A Detailed Analysis of Quicksort
Algorithms with Experimental Mathematics
8All about QuicksortQuicksort
.
25
8
msvc 14.2
x86 Release
C++
Intel i7 7700
RAM 16 GB
26
8.1 HOARE VS LOMUTO
Hoare’s partition lomuto’s partition
10.012 0.015
20.029 0.031
30.033 0.036
40.02 0.021
50.031 0.036
60.009 0.012
70.021 0.023
80.031 0.034
90.017 0.018
100.019 0.02
1: n = 10000000 partition
Hoare’s quick sort lomuto’s quick sort
10.719 0.759
20.704 0.77
30.712 0.764
40.69 0.761
50.698 0.758
60.696 0.759
70.696 0.769
80.692 0.761
90.697 0.765
100.695 0.755
2: n = 10000000 quicksort
partition
않았.
8.2
PARALLELIZATION
insertion sort
3way partition
27
Hoare’s quick sort lomuto’s quick sort
10.025 0.044
20.013 0.04
30.013 0.038
40.014 0.041
50.014 0.042
60.013 0.044
70.015 0.044
80.014 0.041
90.014 0.041
100.016 0.042
3: n = 10000
Hoare’s quick sort lomuto’s quick sort
10.023 0.035
20.015 0.032
30.019 0.037
40.017 0.033
50.012 0.031
60.015 0.036
70.016 0.036
80.014 0.035
90.015 0.034
100.02 0.036
4: n = 10000
insertion sortPARALLELIZATIONlomuto’s partition
.
28
PARALLELIZATION quick sort quick sort + insertion sort(n¡=10)
10.511 0.717
20.605 0.714
30.541 0.715
40.456 0.711
50.727 0.715
60.771 0.719
70.744 0.705
80.643 0.711
90.649 0.715
100.721 0.712
5: n = 10000000 임의
lumoto’s quick sort Dijkstra’s
10.778 0.772
20.774 0.778
30.759 0.777
40.774 0.77
50.761 0.766
60.773 0.778
70.772 0.782
80.767 0.774
90.777 0.784
100.767 0.781
6: n = 10000000 임의
lumoto’s quick sort Dijkstra’s
10.41 0.033
20.39 0.038
30.366 0.032
40.367 0.03
50.36 0.032
60.357 0.032
70.361 0.031
80.358 0.032
90.356 0.031
100.365 0.032
7: n = 10000000 0 1000 위의 함한
29
Dijkstra’s J. Bentley D. McIlroy
10.036 0.032
20.037 0.034
30.036 0.031
40.038 0.036
50.04 0.035
60.036 0.034
70.043 0.031
80.036 0.033
90.038 0.036
100.036 0.031
8: n = 10000000 0 1000 위의 함한
8.3 std::sort VS J. Bentley D. McIlroy
std::sortJ. Bentley D. McIlroy3 way partition
. 3 way partitioninertion sort
.
임의 01000 위의 함한 0
10.964 0.38 0.007
20.927 0.374 0.007
30.959 0.373 0.007
40.95 0.386 0.006
50.946 0.381 0.006
60.947 0.383 0.006
70.947 0.392 0.006
80.929 0.383 0.006
90.932 0.385 0.006
100.957 0.383 0.007
9: n = 10000000 std::sort
30
임의 01000 위의 함한 0
10.985 0.337 0.008
21.011 0.332 0.008
30.979 0.338 0.008
40.956 0.336 0.008
51.019 0.338 0.007
60.974 0.333 0.007
70.979 0.341 0.008
80.973 0.341 0.009
90.98 0.326 0.009
100.98 0.332 0.008
10: n = 10000000 J. Bentley D. McIlroy3 way partition
31
1 quick sort [1]......................... 5
2[1]......................... 7
3 quick sort [1] ............. 10
4 9:1할하[1] ...................... 11
5,[1] .............. 11
6 Comparing Quicksort and Radix Sort by (a) instructions executed
per item sorted (b) time per item sorted, and (c) cache misses per
item sorted. This data is from a paper by LaMarca and Ladner
[1996]. Due to such results, new versions of Radix Sort have been
invented that take memory hierarchy into account, to regain its
algorithmic advantages. Th e basic idea of cache optimizations is
to use all the data in a block repeatedly before it is replaced on a
miss.[2] ................................. 20
7n[3] . . . . . . . 22
8 3-way-partitioning [4] ................... 23
9 Fast 3-way partitioning [4]................. 24
1 n = 10000000 partition ............. 27
2 n = 10000000 quicksort ............. 27
3 n = 10000 ....................... 28
4 n = 10000 ......................... 28
5 n = 10000000 임의 .................... 29
6 n = 10000000 임의 ............... 29
7 n = 10000000 0 1000 위의 함한 ........ 29
8 n = 10000000 0 1000 위의 함한 ........ 30
9 n = 10000000 std::sort................. 30
10 n = 10000000 J. Bentley D. McIlroy3 way partition31
32
https://en.wikipedia.org/wiki/Quicksort
https://cs.stackexchange.com/questions/11458/quicksort-partitioning-hoare-vs-lomuto
https://www.acmicpc.net/blog/view/58
https://www.youtube.com/watch?v=hq4dpyuX4Uw&list=PL52K_8WQO5oUuH06MLOrah4h05TZ4n38l&
index=11
https://en.wikipedia.org/wiki/Recursion_(computer_science)
https://algs4.tistory.com/45
https://arxiv.org/pdf/1905.00118.pdf
http://rohitja.in/lomuto-hoare-partitioning.html
https://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf https:
//en.wikipedia.org/wiki/Dutch_national_flag_problem
https://en.wikipedia.org/wiki/Introsort
https://www.geeksforgeeks.org/internal-details-of-stdsort-in-c/ https:
//stackoverflow.com/questions/44441876/quick-sort-using-stack-in-c
http://fpl.cs.depaul.edu/jriely/ds1/extras/lectures/23Quicksort.
pdf
[1] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clif-
ford Stein. Introduction to Algorithms, Second Edition. MIT Press and
McGraw-Hill, 2001. ISBN 0-262-03293-7.
[2] Patterson, David A./ Hennessy, John L. Computer Organization and
Design, Fourth Edition: The Hardware/Software Interface (The Morgan
Kaufmann Series in Computer Architecture and Design). 2014. ISBN-13
9788994961897, ISBN-10 8994961895
[3] Sedgewick, R. (1978). ”Implementing Quicksort programs”. Comm. ACM.
21 (10): 847–857. doi:10.1145/359619.359631.
[4] Robert Sedgewick, Kevin Wayne Algorithms, 4th Edition, 2001 ISBN-13:
978-0321573513
33