快速排序算法是20世纪十大最伟大的算法之一。它是由C.R.A.Hoare于1962年提出的一种划分交换排序算法,其采用的是分治的思想,分治是将原问题分解成若干与原问题相似的子问题,通过递归的解决这些子问题来最终得出原问题的解。
快速排序是通过一个划分(SPLIT)算法来将待排序序列划分成两部分,其中左边的元素都小于中间的元素,右边的元素都小于中间的元素。然后通过对子数组进行递归划分就可以完成对整个序列的排序。所以划分(SPLIT)算法是快速排序的核心,它的伪代码如下:
算法:SPLIT
输入:数组A[low...high]
输出:1)重新排列的A;2)划分元素A[low]的新位置w;
i ← low
x ← A[low]
for j ← low+1 to high:
if A[j] <= x then:
i ← i+1
if i != j then 互换A[i]和A[j]
end if
end if
end for
互换A[low]和A[i]
w ← i
return A和w
快速排序很容易用递归的方式实现,递归方式的伪代码如下:
算法:QUICKSORT
输入:n个元素的数组A[1...n]
输出:按非降序排列的数组A中的元素
quicksort(A, 1, n)
if low < high then
SPLIT(A[low...high], w)
quicksort(A, low, w-1)
quicksort(A, w+1, high)
end if
快速排序也可以用迭代的方式实现,迭代方式伪代码如下:
算法:QUICKSORT
输入:n个元素的数组A[1...n]
输出:按非降序排列的数组A中的元素
quicksort(A, 1, n)
if low >= high then
return
end if
low 进栈
high 进栈
while 栈不为空:
high = 栈顶(出栈)
low = 栈顶(出栈)
w = SPLIT(A, low, high)
if low < w-1 then:
low 进栈
w-1 进栈
end if
if high > w+1 then:
w+1 进栈
high 进栈
end if
end while
在这里只实现快速排序的迭代方式实现,C++代码实现的快排如下:
#include <iostream>
#include <stack>
#define Type int
using namespace std;
int Split(Type * a, int low, int high) {
int i = low;
int x = a[low];
for (int j = low+1; j <= high; j++) {
if (a[j] <= x) {
i ++;
if (i != j) {
Type temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
Type temp = a[low];
a[low] = a[i];
a[i] = temp;
return i;
}
void QuickSort(Type * a, int low, int high) {
if (low >= high) {
return;
}
stack<int> range;
range.push(low);
range.push(high);
while(!range.empty()) {
high = range.top();
range.pop();
low = range.top();
range.pop();
int w = Split(a, low, high);
if (low < w-1) {
range.push(low);
range.push(w-1);
}
if (high > w+1) {
range.push(w+1);
range.push(high);
}
}
}
int main(void) {
Type a[20] = {2, 3, 12, 18, 7, 8, 4, 9, 14, 12, 5, 2, 3, 17, 15, 7, 9, 11, 13 ,3};
cout << "排序前:\n";
for (int i = 0; i < 20; i ++) {
cout << a[i] << " ";
}
cout << endl;
QuickSort(a, 0, 19);
cout << "迭代版快速排序后:\n";
for (int i = 0; i < 20; i ++) {
cout << a[i] << " ";
}
cout << endl;
getchar();
return 0;
}
Java版本:
package sort;
import java.util.Stack;
public class QuickSort {
private int [] array = null;
private boolean sorted = false;
public QuickSort(int [] a) {
this.array = new int [a.length];
System.arraycopy(a, 0, this.array, 0, a.length);
}
private int split(int low, int high) {
int i = low;
int x = array[low];
for (int j = low+1; j <= high; j++) {
if (array[j] <= x) {
i ++;
if (i != j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
int temp = array[low];
array[low] = array[i];
array[i] = temp;
return i;
}
private void quickSort() {
if (array.length <= 1) {
sorted = true;
return;
}
int low = 0, high = array.length-1;
Stack<Integer> range = new Stack<Integer>();
range.push(low);
range.push(high);
while (!range.empty()) {
high = range.pop();
low = range.pop();
int w = split(low, high);
if (low < w-1) {
range.push(low);
range.push(w-1);
}
if (high > w+1) {
range.push(w+1);
range.push(high);
}
}
}
public int [] getSortedIntArray() {
if (!sorted) {
quickSort();
}
return this.array;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] a = {2, 3, 12, 18, 7, 8, 4, 9, 14, 12, 5, 2, 3, 17, 15, 7, 9, 11, 13 ,3};
System.out.println("排序前:");
for (int i = 0;i < 20; i ++) {
System.out.print(a[i] + " ");
}
QuickSort qs = new QuickSort(a);
a = qs.getSortedIntArray();
System.out.println("迭代版快速排序后:");
for (int i = 0;i < 20; i ++) {
System.out.print(a[i] + " ");
}
}
}
Python版本:
#! /usr/bin/env python
# -*- coding:utf-8 -*-
def split(array, low, high):
i = low
x = array[low]
for j in range(low+1, high+1):
if array[j] <= x:
i = i+1
if i != j:
temp = array[i]
array[i] = array[j]
array[j] = temp
temp = array[low]
array[low] = array[i]
array[i] = temp
return i
def quickSort(array, low, high):
if low >= high:
return
stack_range = list()
stack_range.append(low)
stack_range.append(high)
while len(stack_range) != 0:
high = stack_range.pop()
low = stack_range.pop()
w = split(array, low, high)
if low < w-1:
stack_range.append(low)
stack_range.append(w-1)
if high > w+1:
stack_range.append(w+1)
stack_range.append(high)
if __name__ == '__main__':
a = [2, 3, 12, 18, 7, 8, 4, 9, 14, 12, 5, 2, 3, 17, 15, 7, 9, 11, 13 ,3]
print '排序前:'
for i in a:
print i,
print
quickSort(a, 0, len(a)-1)
print '迭代排序后:'
for i in a:
print i,
鉴于本人水平有限,有错误欢迎指正。
分享到:
相关推荐
知道快速排序算法的思想,但是一直都没有动手写,今天写了下,发现还不是那么容易
快速排序算法,C语言 快速排序(Quicksort)是对冒泡排序的一种改进。由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有...
快速排序算法(C经典实例) 快速排序算法(C经典实例) 快速排序算法(C经典实例) 快速排序算法(C经典实例) 快速排序算法(C经典实例)
c语言版本的数据结构的快速排序算法,适用于新手学习
快速排序算法C语言实现,快排序算法QuickSort.cpp
快速排序算法c++实现,快速实现插入排序十万个数(调用)。可以改成输入。并附加了程序运行计时,用于测试时间复杂度,可以移除。绝对能用
给定一个数列,用快速排序算法把它排成升序
C++快速排序算法程序,用于处理大量数据, 并对这些数据进行快速的排序
自己写的用C++实现的快速排序算法,运行通过,可以作为参考。
快速排序算法 word格式 较快速度 MATLAB格式
快速排序算法的精确实现 已经编译通过 在VC++6.0上面
快速排序算法的改进思路 1.选取好的基准,是两边的数据个数尽量的均匀 取数组的第一个,中间,最后一个数据,取三个数中第二大的数作为基准 2. 不递归 3.与插入结合,当段内的数组个数小于等于16的时候,使用...
2、单处理机上快速排序算法 3、快速排序算法的性能 4、快速排序算法并行化 5、描述了使用2m个处理器完成对n个输入数据排序的并行算法。 6、在最优的情况下并行算法形成一个高度为logn的排序树 7、完成快速排序的...
详细解释了快速排序的java实现.里面有代码,还有注释说明
资源名:matlab 实现快速排序算法_一共有主程序,分类,插入和选择四个程序 资源类型:matlab项目全套源码 源码说明: 全部项目源码都是经过测试校正后百分百成功运行的,如果您下载后不能运行可联系我进行指导或者...
JAVA冒泡排序和快速排序算法,符合实验报告要求哦
经测试的C++快速排序算法,可直接运行 源代码
1)不做随机化处理的递归实现; 2)采用随机化处理的递归实现; 3)用while循环消除尾递归; 4)用栈模拟递归,并证明所需的栈空间为O(logn); 5) 够小时改用插入排序