快速排序

记录下来,以后找方便点

#include<stdio.h>
#include
<stdlib.h>
#include
<time.h>
#define N 100
void QKSort(int r[],int low,int high)
/*对记录数组r[low..high]用快速排序算法进行排序*/
{
int pos;
if(low<high)
{
pos
=QKPass(r,low,high);
QKSort(r,low,pos
-1);//对左部子表进行排序
QKSort(r,pos+1,high);//对右部子表进行排序
}

}
//一趟快速排序算法
int QKPass(int r[],int left,int right)
{
int x=r[left];//选择基准记录
int low=left;
int high=right;
while(low<high)
{
while(low<high&&r[high]>x)
high
--;//high从右到左找小于x的记录
if(low<high)
{r[low]
=r[high];low++;}
while(low<high&&r[low]<x)
low
++;
if(low<high) {r[high]=r[low];high--;}
}
r[low]
=x;
return low;
}
void main()
{
int a[N];
int i;
srand((unsigned)time(NULL));

for(i=0;i<N;i++)
a[i]
=rand()%5000;
printf(
"原始数据:\n");
for(i=0;i<N;i++)
{
printf(
"%5d",a[i]);
if((i+1)%10==0)
printf(
"\n");
}

printf(
"\n排序后的数据:\n");
QKSort(a,
0,N-1);
for(i=0;i<N;i++)
{
printf(
"%5d",a[i]);
if((i+1)%10==0)
printf(
"\n");
}
printf(
"\n");
}
posted @ 2011-07-08 19:08  又是一年夏天  阅读(197)  评论(0)    收藏  举报