快速排序法!
/// E-mail:cangzhu@163.com
//快速排序法
//基本的思想:通过一趟排序将待排的记录分割成独立的两部分,
//其中前一部分的 记录的要害字均比另一部分记录的要害字小,
//再分别对两组记录进行递归分割,达到排序的目的
//平均时间复杂度为 O(log2(n))
#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 { while(low high--; swap(data[low],data[high]); while(low low++; swap(data[low],data[high]); } return low; } //进行的递归的调用,达到排序的目的 void QSort(int data[],int low,int high) { if(low { 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< mydata[i]=rand()%100; cout< } QSort(mydata,0,49); cout< for(i=0;i<50;i++) { //每行显示 10 个数据 if(i%10==0) cout< cout< } }