快速排序
代码
#include<iostream>
using namespace std;
void printList(int *Arr,int ArrLen);
int partition(int s[],int low, int high){
int pivot = s[high];
int i = low-1;
for (int j = low; j <= high-1; j++){
if (s[j] < pivot){
i++;
swap(s[i],s[j]);
}
}
swap(s[i + 1],s[high]);
return (i + 1);
}
int* quickSort(int s[],int low, int high, int listSize){
int pi;
if (low < high){
pi = partition(s,low,high);
printList(s, listSize);
quickSort(s, low, pi-1,listSize);
quickSort(s, pi + 1,high,listSize);
}
return s;
}
void printList(int *Arr,int ArrLen){
for(int i=0;i<ArrLen;i++){
cout<<Arr[i]<<" ";
}
cout<<endl;
}
int main(int argc, char const *argv[])
{
int list[] = {4, 10, 8, 1, 9, 7, 2, 6, 3,5,11};
int listSize = sizeof(list)/sizeof(list[0]);
int* s = quickSort(list,0,listSize-1,listSize);
cout<<"The final sort result:"<<" ";
printList(s, listSize);
return 0;
}
执行效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/15bf1d4885fe4cb69969821f06766cdf.png)