快速排序算法实现代码:

//============================================================================
// 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. gym 100735I

    Description standard input/outputStatements You are given three numbers. Is there a way to replace v ...

  2. 反斜杠处理函数addslashes()和stripslashes()

    addslashes():对输入字符串中的某些预定义字符前添加反斜杠,这样处理是为了数据库查询语句等的需要.这些预定义字符是:单引号 (') ,双引号 (") ,反斜杠 (\) ,NULL. ...

  3. 【 正确使用vim编辑器的姿势】

    vi:可视化接口(Visual Interface) vim:是vi的增强版(vi iMprove) vi编辑器是所有Unix及Linux系统下标准的编辑器,他就相当于windows系统中的记事本一样 ...

  4. cogs 1396. wwww

    1396. wwww ☆   输入文件:wwww.in   输出文件:wwww.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 对于一个递归函数w(a,b,c) 如果 ...

  5. android5.x加入sim1,sim2标识

    1,mobile_signal_group.xml  ..... <FrameLayout android:id="@+id/mobile_combo" android:la ...

  6. SAP学习之路

    此贴记录学习SAP过程~如有错误请指出~ 6.看着网上学习SAP的貌似比較花精力.学习好的话发展前景还是不错的 5.尽管还不是非常懂.可是还是充满期待,期待着java转型abap. 能够在虚拟机上安装 ...

  7. 72.调用req.flash('error', '用户已存在!'); 时候 报错 "req.flash is not a function"

    在app.js 中调用app.use 的顺序有关 app.use(session({ secret: settings.cookieSecret, key: settings.db,//cookie ...

  8. AC自动机 hdu2222

    #include <iostream> using namespace std; struct Node{ Node *next[]; Node* fail; int count; Nod ...

  9. js中Array.prototype.push.call的用法

    var arr = [] Array.prototype.push.call(arr,"a","b","c") <==> []. ...

  10. 经典的横线中间文字css布局---flex布局

    html: <div class="title"> <div class="line"></div> <div cla ...