快排测试程序

#include <iostream> #include <string> #include "stdlib.h" #include "stdio.h" using namespace std; const int ARRSIZE=10000; int partition(int* arr, int low, int high) { int pivot = arr[low]; while(low < high) { while((low < high) && (arr[high] >= pivot)) --high; arr[low] = arr[high]; while((low < high) && (arr[low] <= pivot)) ++low; arr[high] = arr[low]; } arr[low] = pivot; return low; } void quicksort(int* arr, int low, int high) { if((low < high)) { int mid = partition(arr,low,high); if(mid > 0) quicksort(arr,low,mid-1); if(mid < ARRSIZE-1) quicksort(arr,mid+1,high); } else return; } void generateRandom(const char* url) { FILE* f; f = fopen(url,"w"); if(f == NULL) { cout<<"create file error"<<endl; return; } else { for(int i=0;i<ARRSIZE;++i) { int rd = rand(); char buf[20]; itoa(rd,buf,10); fputs(buf,f); fputc(' ',f); } int fc = fclose(f); if(fc == 1) cout<<"file close error"<<endl; } int fc = fclose(f); if(fc == 1) cout<<"file close error"<<endl; } void readData(const char* url, int* arr) { FILE* f; f = fopen(url,"r"); if(f == NULL) { cout<<"read file error"<<endl; return; } else { for(int i=0;i<ARRSIZE;++i) { fscanf(f,"%d",arr+i); } } int fc = fclose(f); if(fc == 1) cout<<"file close error"<<endl; } void getResult(const char* url, int* arr) { FILE* f; f = fopen(url,"w"); for(int i=0;i<ARRSIZE;++i) { char buf[20]; itoa(arr[i],buf,10); fputs(buf,f); fputc(' ',f); } int fc = fclose(f); if(fc == 1) cout<<"file close error"<<endl; } int main(int argc, char* argv[]) { generateRandom(argv[1]); int arr[ARRSIZE]; readData(argv[1], arr); quicksort(arr,0,ARRSIZE-1); getResult(argv[2],arr); return 0; }

算法运行起来后,最多支持十万级的数组排序,对于超过百万的数组,一个想法是分段读取文件,将每段的结果单独存于一个文件,然后使用归并排序。

 

你可能感兴趣的:(测试,File,null,url,include,PIVOT)