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
77
面試面試面試,因為很重要所以要說三次!
kuoe0
2
210
應徵軟體工程師
kuoe0
0
150
面試心得分享
kuoe0
0
390
Windows 真的不好用...
kuoe0
0
250
Python @Wheel Lab
kuoe0
0
180
Introduction to VP8
kuoe0
0
200
Python @NCKU_CSIE
kuoe0
0
95
[ACM-ICPC] Tree Isomorphism
kuoe0
1
220
Other Decks in Programming
See All in Programming
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
Jakarta EE meets AI
ivargrimstad
0
610
距離関数を極める! / SESSIONS 2024
gam0022
0
280
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
170
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
210
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
327
21k
Rails Girls Zürich Keynote
gr2m
94
13k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Ruby is Unlike a Banana
tanoku
97
11k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
The Language of Interfaces
destraynor
154
24k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Docker and Python
trallard
40
3.1k
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 1 6 9 8 3 1 4
5 6 4 5 6 5 6 2 1 2
9 8 2 4 3 5 1 6 2 4 5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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