zoukankan      html  css  js  c++  java
  • 生成100万个数 再排序

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

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

    [cpp] view plaincopy
     
    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 plaincopy
     
    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. }  
  • 相关阅读:
    CTF SQL注入知识点
    Rot13加密算法
    LFU缓存
    Redability
    快排
    更新卡片的zIndex
    webshell文件下载器
    [转]背包九讲
    hihocoder第196周
    Python import容易犯的一个错误
  • 原文地址:https://www.cnblogs.com/xpwzq/p/3303678.html
Copyright © 2011-2022 走看看