#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void swap(int *a,int*b)
{
int temp =*a;
*a=*b;
*b=temp;
}
int random(int st,int end)
{
int length = end-st+1;
return st+rand()%length;
}
int Partition(int st,int end,int data[])
{
int rand = random(st,end);
swap(&data[rand],&data[end]);
int small=st-1,index=st;
for(index=st;index<end;index++)
{
if(data[index]<data[end])
{
small++;
if(small!=index)
{
swap(&data[small],&data[end]);
}
}
}
small++;
swap(&data[small],&data[end]);
return small;
}
void quickSort(int st,int end,int data[])
{
//if(st==end) return;
if(st<=end)
{
int index = Partition(st,end,data);
quickSort(index,end,data);
quickSort(st,index,data);
}
return;
}
void show(int * data,int length)
{
int i=0;
for(i=0;i<length;i++)
{
cout<<data[i]<<" ";
}
}
void main()
{
srand(time(NULL));
int data[]={1,8,3,10,9,879,21};
quickSort(0,7,data);
show(data,7);
}
快速排序算法
最新推荐文章于 2022-09-21 10:12:32 发布