地址:http://www.cppblog.com/0and1/ | E-Mail:firenight@163.com | QQ:79688942 |
C++描述的快速排序……
#include <iostream>
const int B=40;
void quicksort(int left,int right,int num[])
{
int i,j,vot;
if (left<right)
{
i=left;
j=right;
vot=num[i];
while (i!=j)
{
while ((vot<num[j]) && (i<j))
j--;
if (i<j)
{
num[i]=num[j];
i++;
}
while ((vot>num[i]) && (i<j))
i++;
if (i<j)
{
num[j]=num[i];
j--;
}
}
num[i]=vot;
i++;
j--;
quicksort(left,j,num);
quicksort(i,right,num);
}
}
int main()
{
using namespace std;
int num[B];
for (int y=0;y<B;y++) {num[y]=rand()%50;}
for (int m=0;m<B;m++)
cout << num[m] << " ";
cout << endl;
quicksort(0,B-1,num);
for (int n=0;n<B;n++)
cout << num[n] << " " ;
cout << endl;
return 0;
}