#ifndef _PAIXU_H
#define _PAIXU_H
#define N 15
int quick_sort(int *data,int low,int high);
int partion(int *data,int low,int high);
#endif
#include <stdio.h>
#include "paixu.h"
#include <string.h>
#include <stdlib.h>
int quick_sort(int *data,int low,int high){
int t;
if(data==NULL) return -1;
if(low>=high) return 0;
t=partion(data,low,high);
quick_sort(data,low,t-1);
quick_sort(data,t+1,high);
return 0;
}
int partion(int *data,int low,int high){
int temp;
temp=data[low];
while(low<high){
while(low<high&&temp<=data[high]){
high--;
}
data[low]=data[high];
while(low<high&&temp>=data[low]){
low++;
}
data[high]=data[low];
}
data[low]=temp;
return low;
}
#include <stdio.h>
#include "paixu.h"
#include <string.h>
#include <stdlib.h>
int main(int argc,const char *argv[])
{
int data[N]={0};
int i;
srandom(9);
for(i=0;i<N;i++){
data[i]=random()%100;
}
for(i=0;i<N;i++){
printf("%d ",data[i]);
}
printf("\n");
quick_sort(data,0,N-1);
for(i=0;i<N;i++){
printf("%d ",data[i]);
}
return 0;
}
快速排序数据结构程序代码
最新推荐文章于 2024-06-11 17:38:20 发布