简介: 快速排序算法是一个典型的采用分治思想的算法,时间复杂度为O(nlogn),下面先来看看它的递归实现:
Java版本
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {9,8,7,6,5,4,3,2,1};
quickSort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
public static void quickSort(int[] a,int start,int end){
if (start < end){
int mid = partion(a,start,end);
qSort(a,start,mid-1);
qSort(a,mid+1,end);
}
}
public static int partion(int[] a,int start,int end){
int x = a[start],j = start;//选择第一个作为基准
//使得在区间[start,end]所有小于x的位于x的左边,大于x的位于右边
for (int i = start+1; i <=end; i++) {
if (a[i] < x){
j++;
swap(a,i,j);
}
}
swap(a,start,j);
return j;
}
public static void swap(int[] a,int i,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
C++版本
#include <bits/stdc++.h>
#include <algorithm>
#define GET_ARRAY_LEN(array,len){len = (sizeof(array) / sizeof(array[0]));}
using namespace std;
int partion(int a[],int start,int end){
int x = a[start],j = start;
for (int i = start + 1; i <= end; ++i) {
if (a[i] < x){
j++;
swap(a[i],a[j]);
}
}
swap(a[start],a[j]);
return j;
}
void quickSort(int a[],int start,int end){
if (start < end){
int mid = partion(a,start,end);
quickSort(a,start,mid - 1);
quickSort(a,mid + 1,end);
}
}
int main() {
int a[] = {9,8,7,6,5,4,3,2,1};
int len;
GET_ARRAY_LEN(a,len);
quickSort(a,0,len - 1);
for (int i = 0; i < len; ++i) {
cout << a[i] << " ";
}
cout <<endl;
return 0;
}
其中核心的方法为partion,算法中主要通过该方法对数组元素进行了交换,每调用一次partion方法,都会使得你所选择的基元元素x的左边均小于x,右边均大于x
下面给出非递归的实现代码
Java版本
import java.util.ArrayDeque;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {9,8,7,6,5,4,3,2,1};
quickSort(a);//非递归
System.out.println(Arrays.toString(a));
}
public static int partion(int[] a,int start,int end){
int x = a[start],j = start;
for (int i = start+1; i <=end; i++) {
if (a[i] < x){
j++;
swap(a,i,j);
}
}
swap(a,start,j);
return j;
}
public static void swap(int[] a,int i,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void quickSort(int[] a){
int len;
if (a == null || (len = a.length - 1) == 0) return;
ArrayDeque<Section> stack = new ArrayDeque<>();
Section section = new Section(0,len);
stack.addLast(section);
while (!stack.isEmpty()){
section = stack.removeLast();
int mid = partion(a,section.start,section.end);
if (section.start < mid - 1) stack.addLast(new Section(section.start,mid - 1));
if (mid + 1 < section.end) stack.addLast(new Section(mid + 1,section.end));
}
}
}
//区间类
class Section{
int start,end;
public Section(){}
public Section(int start,int end){
this.start = start;
this.end = end;
}
}
C++版本
#include <bits/stdc++.h>
#include <algorithm>
#define GET_ARRAY_LEN(array,len){len = (sizeof(array) / sizeof(array[0]));}
using namespace std;
//区间
struct Section{
int start;
int end;
};
int partion(int a[],int start,int end){
int x = a[start],j = start;
for (int i = start + 1; i <= end; ++i) {
if (a[i] < x){
j++;
swap(a[i],a[j]);
}
}
swap(a[start],a[j]);
return j;
}
void quickSort(int a[],int len){
int start = 0,end = len - 1;
stack<Section> stk;
Section section;
section.start = 0;
section.end = end;
stk.push(section);
while (!stk.empty()){
section = stk.top();
stk.pop();
int mid = partion(a,section.start,section.end);
Section temp;
if (section.start < mid - 1){
temp.start = section.start;
temp.end = mid - 1;
stk.push(temp);
}
if (mid + 1 < section.end){
temp.start = mid + 1;
temp.end = section.end;
stk.push(temp);
}
}
}
int main() {
int a[] = {9,8,7,6,5,4,3,2,1};
int len;
GET_ARRAY_LEN(a,len);
quickSort(a,len);
for (int i = 0; i < len; ++i) {
cout << a[i] << " ";
}
cout <<endl;
return 0;
}
非递归实现中,借助了一种数据结构——栈(后进先出),同时新增了一个区间类,用于存储需要排序的区间,核心方法还是partion。