Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Quick Sort
Search
KuoE0
March 15, 2013
Programming
5
10k
Quick Sort
KuoE0
March 15, 2013
Tweet
Share
More Decks by KuoE0
See All by KuoE0
Protocol handler in Gecko
kuoe0
0
81
面試面試面試,因為很重要所以要說三次!
kuoe0
2
220
應徵軟體工程師
kuoe0
0
150
面試心得分享
kuoe0
0
400
Windows 真的不好用...
kuoe0
0
260
Python @Wheel Lab
kuoe0
0
190
Introduction to VP8
kuoe0
0
210
Python @NCKU_CSIE
kuoe0
0
100
[ACM-ICPC] Tree Isomorphism
kuoe0
1
230
Other Decks in Programming
See All in Programming
CloudNativePGがCNCF Sandboxプロジェクトになったぞ! 〜CloudNativePGの仕組みの紹介〜
nnaka2992
0
140
AHC041解説
terryu16
0
510
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
450
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
230
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
shadcn/uiを使ってReactでの開発を加速させよう!
lef237
0
350
ecspresso, ecschedule, lambroll を PipeCDプラグインとして動かしてみた (プロトタイプ) / Running ecspresso, ecschedule, and lambroll as PipeCD Plugins (prototype)
tkikuc
2
2.1k
チームの立て直し施策をGoogleの 『効果的なチーム』と見比べてみた
maroon8021
0
150
SRE、開発、QAが協業して挑んだリリースプロセス改革@SRE Kaigi 2025
nealle
1
2.8k
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
2.3k
2025.01.17_Sansan × DMM.swift
riofujimon
2
630
Azure AI Foundryのご紹介
qt_luigi
1
240
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
Optimising Largest Contentful Paint
csswizardry
33
3k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Why Our Code Smells
bkeepers
PRO
335
57k
Designing for humans not robots
tammielis
250
25k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The World Runs on Bad Software
bkeepers
PRO
66
11k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Unsuck your backbone
ammeep
669
57k
Transcript
Quick Sort ֲࢸݢʢKuoE0ʣ
[email protected]
KuoE0.ch
Latest update: Mar 15, 2013 Attribution-ShareAlike 3.0 Unported (CC BY-SA
3.0) http://creativecommons.org/licenses/by-sa/3.0/
Algorithm ࠾༻ divide & conquer ࡦུ
Divide ኺᏐྻதग़Ұݸݩૉ࡞ҝ pivotɼར ༻ pivot ሡݪᏐྻҝၷݸࢠᏐྻɿ • ॴ༗ݩૉখԙ pivot తᏐྻ
A • ॴ༗ݩૉେԙ pivot తݩૉతᏐྻ B • ԙ pivot తݩૉՄ์ஔࡏҰݸத
2 9 4 3 8 7 5 1 6
2 9 4 3 8 7 5 1 6 2
7 4 3 5 1 6 9 8
9 8 2 4 3 5 1 6 2 9
4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8
9 8 2 4 3 5 1 6 2 4
5 1 6 2 9 4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8 3
4 5 6 2 1 9 8 2 4 3
5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8 3
4 5 6 2 1 2 9 8 2 4
3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8 3 1
4 5 6 2 1 2 9 8 2 4
3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 2 7 4 3 5 1 6 9 8 3 1
4 5 6 5 6 2 1 2 9 8
2 4 3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 2 7 4 3 5 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 5 9 1 6 4 3 8
8 9 1 2 3 4 5 6 8 9
4 5 6 5 6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 9 2 5 2 5 9 7 1 6 4 3 8
8 9 1 2 3 4 5 6 8 9
4 5 6 5 6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 9 2 5 2 5 9 7 1 6 4 3 8
8 9 1 2 3 4 5 6 8 9
4 5 6 5 6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 9 2 5 2 5 9 7 1 6 4 3 8
9 3 How to Divide 4 1 5 8 6
7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2
9 3 How to Divide 4 1 5 8 6
4 7 2 4 1 3 2 5 8 7 9 6
5 6 7 8 9 3 2 1 4 How
to Conquer
5 6 7 8 9 3 2 1 4 How
to Conquer
5 6 7 8 9 3 2 1 4 How
to Conquer
5 6 7 8 9 3 2 1 4 How
to Conquer
void quickSort( int L, int R ) { if (
R - L <= 1 ) return; int pivot = N[ R - 1 ], p = L; for ( int i = L; i < R - 1; ++i ) { if ( N[ i ] <= pivot ) { swap( N[ i ], N[ p ] ); ++p; } } swap( N[ R - 1 ], N[ p ] ); quickSort( L, p ); quickSort( p + 1, R ); } In-place Version
9 4 3 6 5 1 8 7 How to
Divide 2
9 4 3 6 5 1 8 7 How to
Divide 2 6
9 4 3 6 5 1 8 7 How to
Divide 2 2 6
9 4 3 6 5 1 8 7 How to
Divide 2 2 6
9 4 3 6 5 1 8 7 How to
Divide 2 2 2 6
9 9 4 3 6 5 1 8 7 How
to Divide 2 2 2 6
9 9 4 4 3 6 5 1 8 7
How to Divide 2 2 2 9 6
9 9 4 4 3 6 5 1 8 7
How to Divide 2 2 2 9 6
9 9 4 4 3 6 5 1 8 7
How to Divide 2 2 2 9 4 6
9 9 4 4 3 3 6 5 1 8
7 How to Divide 2 2 2 9 4 6
9 9 4 4 3 3 6 5 1 8
7 How to Divide 2 2 2 9 4 6
9 9 4 4 3 3 6 5 1 8
7 How to Divide 2 2 2 9 4 3 6
9 9 4 4 3 3 6 5 1 8
7 8 How to Divide 2 2 2 9 4 3 6
9 9 4 4 3 3 6 5 1 8
7 8 8 How to Divide 2 2 2 9 4 3 6
9 9 4 4 3 3 6 5 1 8
7 8 7 8 How to Divide 2 2 2 9 4 3 6
9 9 4 4 3 3 6 5 1 8
7 8 7 8 7 How to Divide 2 2 2 9 4 3 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 9 4 3 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 9 4 3 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 1 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 1 6
9 9 4 4 3 3 6 5 1 5
8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 1 6 2 5 4 3 1 8 9 7
6 How to Conquer 1 4 2 3 5 8
7 9 1 4 2 3 5 8 7 9
6 How to Conquer 1 4 2 3 5 8
7 9 1 4 2 3 5 8 7 9
End