快速排序算法实现代码:

//============================================================================
// 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的更多相关文章

  1. C/C++ Quick Sort Algorithm

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255069 快速排序算法,由C.A. ...

  2. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  3. PAT1101:Quick Sort

    1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...

  4. A1101. Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  5. 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  6. PAT甲1101 Quick Sort

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  7. PAT 1101 Quick Sort[一般上]

    1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...

  8. 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 ...

  9. PAT 甲级 1101 Quick Sort

    https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...

随机推荐

  1. 联想 Thinkserver TS250服务器RAID1 重建测试

    1.RAID1状态下,拨掉其中一块硬盘后,RAID1即失效. 2.重新插入后,在进行系统后会自动重建 *RAID1 提示Rebuild *进入桌面后软件,显示重建进度 软件下载地址:https://p ...

  2. DEDECMS教程:列表页缩略图随机调用

    如果用过DEDECMS的朋友应该都知道,有些模板列表页面需要用到缩略图,调用内容中的缩略图可以使用系统自带的脚本调用第一张图片.但是,并不是我们所有的内容里都有图片,有时候第一张图片也不一定是适合尺寸 ...

  3. WPF和WinForm的区别, 数据驱动与事件驱动的优势对比

    Winform中针对界面的元素进行操作, 所有业务都关联在当前窗口的后台, 而在此之前, 无奈你是双击事件的添加方式.还是后台绑定事件的方式, 你都需要给每个元素一个固定规范的名称, 然后进行相关的数 ...

  4. tsp问题——遗传算法解决

    TSP问题最简单的求解方法是枚举法. 它的解是多维的.多局部极值的.趋于无穷大的复杂解的空间.搜索空间是n个点的全部排列的集合.大小为(n-1)! .能够形象地把解空间看成是一个无穷大的丘陵地带,各山 ...

  5. iOS 平台上常见的安装包有三种,deb、ipa 和 pxl

    前言:目前 iOS 平台上常见的安装包有三种,deb.ipa 和 pxl. 其中 deb 格式是 Debian 系统(包含 Debian 和 Ubuntu )专属安装包格式,配合 APT 软件管理系统 ...

  6. Levmar:Levenberg-Marquardt非线性最小二乘算法

    Levmar:Levenberg-Marquardt非线性最小二乘算法 eryar@163.com Abstract. Levmar is GPL native ANSI C implementati ...

  7. android framework 02

    Android底层开发1.安装Ubuntu系统2.Ubuntu配置开发环境: sudo apt-get install git-core gnupg flex bison gperf zip sudo ...

  8. Http协议简介【转】

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  9. RadioButton的check改变的时候

    https://stackoverflow.com/questions/8095256/asp-net-radio-button-change You'll need to specify the a ...

  10. 51Nod 1006 最长公共子序列Lcs问题 模板题

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这两个 ...