Quick Sort Algorithm
快速排序算法实现代码:
//============================================================================
// Name : QuickSort.cpp
// Author : Danny
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
using namespace std; void swap(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
} int position(int a[], int left, int right) {
int pivot = left;
int i = left;
int j = right;
while (i < j) { while (i < j && a[j] >= a[pivot]) {
j--;
} if (i < j && a[j] < a[pivot]) {
swap(a, pivot, j);
pivot = j;
} while (i < j && a[i] <= a[pivot]) {
i++;
}
if (i < j && a[i] > a[pivot]) {
swap(a, pivot, i);
pivot = i;
} }
return pivot;
} void quickSort(int a[], int left, int right) {
if (left >= right)
return;
int pos = position(a, left, right);
quickSort(a, left, pos - );
quickSort(a, pos + , right); } int main() {
int a[] = { ,,,,,,,};
quickSort(a, , );
for (int i = ; i < ; i++) {
cout << a[i] << endl;
}
return ;
}
Java实现代码:
排序类:
package algorithm;
public class SortAlgorithm {
void quickSort(int a[], int left, int right) {
if (left >= right)
return;
int pos = position(a, left, right);
quickSort(a, left, pos - 1);
quickSort(a, pos + 1, right);
}
void swap(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int position(int a[], int left, int right) {
int pivot = left;
int i = left;
int j = right;
while (i < j) {
while (i < j && a[j] >= a[pivot]) {
j--;
}
if (i < j && a[j] < a[pivot]) {
swap(a, pivot, j);
pivot = j;
}
while (i < j && a[i] <= a[pivot]) {
i++;
}
if (i < j && a[i] > a[pivot]) {
swap(a, pivot, i);
pivot = i;
}
}
return pivot;
}
}
主方法类:
package algorithm;
public class QuickSort {
public static SortAlgorithm sa;
public static void main(String[] args) {
// TODO Auto-generated method stub
sa=new SortAlgorithm();
int[] a={4,2,1,6,3};
sa.quickSort(a, 0, 4);
for(int i:a){
System.out.println(i);
}
}
}
Quick Sort Algorithm的更多相关文章
- C/C++ Quick Sort Algorithm
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255069 快速排序算法,由C.A. ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT1101:Quick Sort
1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...
- A1101. Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- 1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT甲1101 Quick Sort
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT 1101 Quick Sort[一般上]
1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...
- What does Quick Sort look like in Python?
Let's talk about something funny at first. Have you ever implemented the Quick Sort algorithm all by ...
- PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...
随机推荐
- 路径+DataRow+SqlPara防止sql注入
绝对路径:是从盘符开始的路径,形如C:\windows\system32\cmd.exe相对路径:是从当前路径开始的路径,假如当前路径为C:\windows要描述上述路径,只需输入system32\c ...
- ipad无法连接到app store怎么办
之前入手的air2提示无法连接到app store:你需要首先更新系统到最新的ios版本,去通用设置里面,有个update software, 点击即可,然后才能用apple id 联入,否选择提示连 ...
- vue中剖析中的一些方法
1 判断属性 71 -81 var hasOwnProperty = Object.prototype.hasOwnProperty; /** * Check whether the object h ...
- 关于viewport详解
- A电脑能ping 通B电脑,但B电脑无法ping能和访问B
过程: A电脑共享了打印机,B电脑想连接A的打印机,发现拒绝访问,无法登录A电脑 问题: A电脑能ping 通B电脑,但B电脑无法ping能和访问B 原因:A电脑禁用了共享访问 处理:使用一键共享设置 ...
- sleep实现原理
用户程序中的睡眠: sleep() usleep() nanosleep() sleep()和nanosleep()都是使进程睡眠一段时间后被唤醒,但是二者的实现完全不同.Linux中并没 ...
- NOIP2009 最优贸易(BFS)
本题正解是tarjan.我没有去写 之前的代码是错误的不好意思,因为数据太弱一直没有发现. 相同还是两遍bfs,一次正向,一次反向.在正向的时候我们求出从起点走到各个点的最小值.在反向的时候求出从终点 ...
- 论Nim中的 proc 和 method
在Nim中.proc 是定义过程的keyword.method 是定义方法的keyword.它们之间根本的差别是proc定义的过程是静态绑定.method定义的方法是动态绑定.谈到静态绑定.动态绑定又 ...
- HDU 2886 Lou 1 Zhuang
思维好重要.. 对于n+m == k , 当n == m || abs(n-m) == 1 时n*m取得最大值. 设 s = x*(l-x),s = lx-x^2.其导函数为 s' = -1/2x + ...
- 带你彻彻底底弄懂Scroller
Scroller的使用 这是一个滑动帮助类.并不能够使View真正的滑动,而是依据时间的流逝.获取插值器中的数据.传递给我们.让我们去配合scrollTo/scrollBy去让view产生缓慢滑动,产 ...