- #include <iostream>
- #include <algorithm>
- void print(int *a, int n)
- {
- int i = 0;
- while(i < n){
- std::cout << a[i] << ",";
- i++;
- }
- std::cout << "\n";
- }
- int partition(int *arr, const int left, const int right) {
- const int mid = left + (right - left) / 2;
- const int pivot = arr[mid];
- // move the mid point value to the front.
- std::swap(arr[mid],arr[left]);
- int i = left + 1;
- int j = right;
- while (i <= j) {
- while(i <= j && arr[i] <= pivot) {
- i++;
- }
- while(i <= j && arr[j] > pivot) {
- j--;
- }
- if (i < j) {
- std::swap(arr[i], arr[j]);
- }
- }
- std::swap(arr[i - 1],arr[left]);
- return i - 1;
- }
- void quicksort(int *arr, const int left, const int right, const int sz){
- if (left >= right) {
- return;
- }
- int part = partition(arr, left, right);
- std::cout << "QSC:" << left << "," << right << " part=" << part << "\n";
- print (arr, sz);
- quicksort(arr, left, part - 1, sz);
- quicksort(arr, part + 1, right, sz);
- }
- int main() {
- int arr[8] = {110, 5, 10,3 ,22, 100, 1, 23};
- int sz = sizeof(arr)/sizeof(arr[0]);
- print(arr, sz);
- quicksort(arr, 0, sz - 1, sz);
- print(arr, sz);
- return 0;
- }
Blog Archive
-
▼
2017
(115)
-
▼
January
(21)
- algorithm - Counting number of comparisons in Quic...
- 谷歌CEO皮查伊:语音搜索是最自然的交互 我们优势明显 - 未名空间(mitbbs.com)
- Count Inversions in an array | Set 1 (Using Merge ...
- Count Inversions in an array | Set 1 (Using Merge ...
- Algs4-1.4.18 Find local minimum in n x n matrix in...
- 理财不理财,差距究竟有多大? - 未名空间(mitbbs.com)
- Urban Dictionary: quote unquote
- Quick Sort
- What are the top 10 algorithms of the 20th century...
- Virgin Founder Reposts Fake Final Words of Steve J...
- How to install matlab on ubuntu
- rizar/attention-lvcsr: End-to-End Attention-Based ...
- How did Strassen come up with his famous Strassen ...
- Yu's Coding Garden : leetcode Questions: Number of...
- WA State Licensing (DOL) Official Site:Moving? Get...
- WA State Licensing (DOL) Official Site: Moving? Ge...
- Compliance and eDiscovery Solutions & Speech Recog...
- Zigin a better job
- 深度丨阿里云AI专家陈一宁:解密阿里云语音识别的计算方案、声学模型和产品落地
- Kaldi 入门详解 - 道老师机器学习专栏 - 博客频道 - CSDN.NET
- 怎样在北美成为中医师,针灸师?
-
▼
January
(21)
Thursday, January 19, 2017
Quick Sort
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment