随机生成100万个数,排序后保存在文件中

转自:http://blog.csdn.net/hackbuteer1/article/details/6574908

随机生成100万个数,存储在文件out1.txt中,使用内部排序完成,并重新储存在文件out2.txt中。

(一)使用STL中的qsort函数进行操作:

[cpp]  view plain copy
  1. #include "stdio.h"  
  2. #include "string.h"  
  3. #include "stdlib.h"  
  4. #include "time.h"  
  5. int a[1000000];  
  6. void load(char filename[]) //写文件  
  7. {  
  8.     int i;  
  9.     FILE *fp;  
  10.     fp=fopen(filename,"w");  
  11.     if(fp==NULL)  
  12.     {  
  13.         printf("cannot open file/n");  
  14.         return;  
  15.     }  
  16.     for(i=0;i<1000000;i++)    
  17.     fprintf(fp,"%d ",a[i]);     
  18. }  
  19. int cmp(const void *a, const void *b)  
  20. {  
  21.     return (*(int*)a)-(*(int*)b); //从小到大进行排序  
  22. }  
  23. void paixu()  
  24. {  
  25.     qsort(a,1000000,sizeof(int),cmp);  
  26. }  
  27. int main(void)  
  28. {  
  29.     int i;  
  30.     char filename[20];  
  31.     srand( (unsigned)time( NULL ) );         //初始化随机数  
  32.     for(i=0;i<1000000;i++)                //打印出10个随机数  
  33.         a[i]=rand();  
  34.     strcpy(filename,"out1.txt");  
  35.     load(filename);  
  36.     paixu();//快速排序  
  37.     strcpy(filename,"out2.txt");  
  38.     load(filename);  
  39.     system("pause");  
  40.     return 0;  
  41. }  

(二)递归实现qsort函数进行操作:

[cpp]  view plain copy
  1. #include "string.h"  
  2. #include "stdlib.h"  
  3. #include "stdio.h"  
  4. #include "time.h"  
  5. int a[1000000];  
  6. void load(char filename[]) //写文件  
  7. {  
  8.     int i;  
  9.     FILE *fp;  
  10.     fp=fopen(filename,"w");  
  11.     if(fp==NULL)  
  12.     {  
  13.         printf("cannot open file/n");  
  14.         return;  
  15.     }  
  16.     for(i=0;i<1000000;i++)   
  17.         fprintf(fp,"%d ",a[i]);    
  18. }  
  19. int partitions(int a[],int low,int high)     
  20. {  
  21.     int pivotkey=a[low];   //基准  
  22.     while(low<high)  
  23.     {  
  24.         while(low<high && a[high]>=pivotkey)  
  25.             --high;  
  26.         a[low]=a[high];  
  27.         while(low<high && a[low]<=pivotkey)  
  28.             ++low;  
  29.   
  30.         a[high]=a[low];  
  31.     }  
  32.     a[low]=pivotkey;  
  33.     return low;  
  34. }     
  35. void qsort(int a[],int low,int high)   //快速排序  
  36. {  
  37.     int pivotkey;  
  38.     if(low<high)  
  39.     {  
  40.         //递归调用  
  41.         pivotkey=partitions(a,low,high);  
  42.         qsort(a,low,pivotkey-1);  
  43.         qsort(a,pivotkey+1,high);  
  44.     }  
  45. }  
  46. int main(void)  
  47. {  
  48.     int i;  
  49.     char filename[20];  
  50.     srand( (unsigned)time( NULL ) );         //初始化随机数  
  51.     for(i=0;i<1000000;i++)                //打印出10个随机数  
  52.         a[i]=rand();  
  53.     strcpy(filename,"out1.txt");  
  54.     load(filename);  
  55.     qsort(a,0,1000000);  //快速排序  
  56.     strcpy(filename,"out2.txt");  
  57.     load(filename);  
  58.     system("pause");  
  59.     return 0;  
  60. }  


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值