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 ...
随机推荐
- Fedora 29 Linux发行版发布,新功能使Web开发人员的工作更方便
Matthew Miller宣布发布Fedora 29.这个项目的最新版本是在Fedora Core 1发布后几乎整整15年才发布的,并且可以在多个版本中用于多个体系结构. 最新版本的Fedora已经 ...
- 【TC SRM 718 DIV 2 A】RelativeHeights
[Link]: [Description] 给你n个数字组成原数列; 然后,让你生成n个新的数列a 其中第i个数列ai为删掉原数列中第i个数字后剩余的数字组成的数列; 然后问你这n个数列组成的排序数组 ...
- Linux中去除windows文件中的控制字符
Windows下的文本文件拿到Linux下时,会在文本行最后面出现很多字符:^M Linux下去除掉的方法是:dos2unix file(需要软件包dos2unix) 当然逆转的方法为unix2dos ...
- jsapi微信支付v3版
请看清楚你的微信支付是v2还是v3.在这里整理的是v3的,v2的同学请忽略! 前期准备须要用的是商户证书,用的是p12的.设置api密钥(在微信商户端中设置),还须要在微信公众号中设置jsapi授权文 ...
- C++ 递归位置排列算法及其应用
废话不多说,我们先看一下位置排序的算法: #include <iostream> using namespace std; int n = 0; int m = 2; int l = 0; ...
- vim 基础学习之查找
普通模式下 /->正向查找 n-向下查找 N-向上查找 ?->反向查找 N-向下查找 n-向上查找 <C-r><C-w> <C-r>-引用,例如引用寄存 ...
- ajax模仿iframe
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CHARINDEX,REPLACE,LEFT+四大系统函数+模糊查询
select CHARINDEX('bob','my name is bob',1)--返回12 bob的第一个b在字符串中排第12(从1开始数) select CEILING(456.4)--45 ...
- 【MongoDB】mongodump and mongorestore of mogodb
The another tool will be mentioned in this blog, namely mongodump and mongorestore. General speaking ...
- 前台技术--div的隐藏与显示
怎样使用页面元素隐藏或显示. HTML为我们提供了两个变量visibility和display visibility:隐藏要元素可是元素所暂用的空间不予释放.也就是说元素隐藏了,可是页面上会流出一片空 ...