#include "iostream.h" #include "stdlib.h"
//交换两变量值 void swap(int &a,int &b) { int c; c=a;a=b;b=c; }
//将数组分成两部分,前一部分的值均比后一部分值小 //返回分界点 int Partition(int data[],int low,int high) { int pivokey; pivokey=data[low]; while(low<high) { while(low<high&&data[high]>=pivokey) high--; swap(data[low],data[high]);
while(low<high&&data[low]<=pivokey) low++; swap(data[low],data[high]); } return low; }
//进行的递归的调用,达到排序的目的 void QSort(int data[],int low,int high) { if(low<high) { int pivokey=Partition(data,low,high); QSort(data,low,pivokey-1); QSort(data,pivokey+1,high); } }
void main() { int i; int mydata[50]; for(i=0;i<50;i++) { //每行显示 10 个数据 if(i%10==0) cout<<endl; mydata[i]=rand()%100; cout<<mydata[i]<<" "; }
QSort(mydata,0,49);
cout<<endl<<endl; for(i=0;i<50;i++) { //每行显示 10 个数据 if(i%10==0) cout<<endl; cout<<mydata[i]<<" "; } } |