求职基础复习之快速排序c++版

 1 #include<iostream>

 2 using namespace std;

 3 int partition(int a[],int p,int q){

 4 int x = a[q];

 5 int i = p-1;

 6 for(int j = p;j<q;j++)

 7 {

 8 if(a[j]<x)

 9 {

10 i++;

11 int temp = a[j];

12 a[j] = a[i];

13 a[i] = temp;

14 }

15 }

16 a[q]=a[i+1];

17 a[i+1]=x;

18 return i+1;

19 }

20 void qsort(int a[],int p,int q){

21 if(p<q){

22 int i = partition(a,p,q);

23 qsort(a,p,i-1);

24 qsort(a,i+1,q);

25 }

26 }

27 

28 int main(){

29 int a[6] = {9,-9,3,-3,0,0};

30 qsort(a,0,5);

31 for(int i = 0;i<6;i++)

32 cout<<a[i]<<"  ";

33 }

 

你可能感兴趣的:(快速排序)