快速排序算法实现代码:

//============================================================================
// 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. [Chromium文档转载,第006章]Chrome IPC To Mojo IPC Cheat Sheet

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Chrome IPC To Mojo IPC Cheat Sheet 目录 1 O ...

  2. mini vimrc

    Mini version: set enc=utf-8 ffs=unix,dos,mac lm=zh_CN.utf-8 set nu nowb nocp nowrap ru nobk sm is no ...

  3. Codeforces Round #240 (Div. 2) 题解

    A: 1分钟题,往后扫一遍 int a[MAXN]; int vis[MAXN]; int main(){ int n,m; cin>>n>>m; MEM(vis,); ; i ...

  4. vuex requires a Promise polyfill in this browser.--ie-vue-兼容处理日记

    1.ie9+报错vuex requires a Promise polyfill in this browser. 解决如下: npm install --save-dev -polyfill 修改c ...

  5. MOOC使用心得

    1. Mooctest 使用心得 慕测平台是编程类考试和练习的服务平台,教师可以轻松监管考试流程,学生可以自由练习编程.系统负责编程练习的自动化评估及可视化展现,配合当下红火的MOOC慕课课程,慕测平 ...

  6. 安卓MP3播放器开发实例(3)之进度条和歌词更新的实现

    上一次谈了音乐播放的实现,这次说下最复杂的进度条和歌词更新.因为须要在播放的Activity和播放的Service间进行交互,所以就涉及了Activity对Service的绑定以及绑定后数据的传输,这 ...

  7. 改动UINavigationBar (导航栏)上NavigationBarItem 的字体大小和颜色的用法

    //创建一个左边button UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"<" ...

  8. 关于命令行签名时.SF和.RSA文件的命名问题

    准备工作: 签名文件名称为android.keystore 签名的别名为123456789.keystore 1.使用签名命令后例如以下图 发现.SF和.RSA文件自己主动命名为12345678.SF ...

  9. 3.cocos代码入口

    模拟代码进入过程: main.cpp #include <iostream> #include "AppDelegate.h" #include "CCApp ...

  10. POJ 3039 搜索??? (逼近)

    思路: 抄的题解 这叫搜索? 难以理解 我觉得就是枚举+逼近 //By SiriusRen #include <cmath> #include <cstdio> #includ ...