第三周作业——冒泡排序和归并排序

  1. #include <iostream>  
  2. #include <fstream>  
  3. #include <time.h>  
  4. using namespace std;  
  5.   
  6. #define TXT_NUM_COUNT 1000001  
  7.   
  8. int ImportToArray(const char *filename,int *array)  //将filename内的纯数字文本导入数组array;  
  9. {  
  10.     int count=0;  
  11.     ifstream fin;  
  12.     fin.open(filename);  
  13.     if( fin.fail() )  
  14.     {  
  15.         cout<<"file read fail!"<<endl;  
  16.         return -1;  
  17.     }  
  18.     while( fin.eof() ==false )  
  19.         fin>>array[count++];  
  20.     fin.close();  
  21.     return count;  
  22. }  
  23.   
  24. int ExportWithArray(const char *filename,int *array,int count)  
  25. {  
  26.     ofstream fout;  
  27.     fout.open(filename);  
  28.     if(fout.fail())  
  29.     {  
  30.         cout<<"file wirte fail"<<endl;  
  31.         return -1;  
  32.     }  
  33.     for(int i=0;i<count;i++)  
  34.         fout<<array[i]<<endl;  
  35.     fout.close();  
  36. }  
  37.   
  38. void bubbleSort(int *array,int count) //冒泡排序法  
  39. {  
  40.     int t;  
  41.     for(int i=0;i<count-1;i++)  
  42.     {  
  43.         for(int j=0;j<(count-i-1);j++)  
  44.             if(array[j]>array[j+1])  
  45.             {  
  46.                 /*array[j]=array[j]^array[j+1]; 
  47.                 array[j+1]=array[j]^array[j+1]; 
  48.                 array[j]=array[j]^array[j+1]; */  
  49.                 t=array[j];  
  50.                 array[j]=array[j+1];  
  51.                 array[j+1]=t;  
  52.             }  
  53.         cout<<i<<endl;  
  54.     }  
  55. }  
  56.   
  57. void merge(int *left,int lsize,int *right,int rsize)        //来自wiki百科  
  58. {  
  59.     int i,j,k;  
  60.     i=j=k=0;  
  61.     int *TempArray=new int[lsize+rsize];  
  62.   
  63.     while(i<lsize && j<rsize)  
  64.     {  
  65.         //较小的数据放到临时数组  
  66.         TempArray[k++]=left[i]<right[j] ? left[i++] : right[j++];  
  67.     }  
  68.   
  69.     //把剩下的元素复制到临时数组  
  70.     while (i<lsize)  
  71.         TempArray[k++]=left[i++];  
  72.     while (j<rsize)  
  73.         TempArray[k++]=right[j++];  
  74.   
  75.     //将临时数组放到left,即原数组的最左边  
  76.     for(int g=0;g<(lsize+rsize);g++)  
  77.         left[g]=TempArray[g];  
  78.   
  79.     delete [] TempArray;        //销毁临时数组  
  80. }  
  81.   
  82. void  mergeSort(int *array,int size)                //来自wiki百科  
  83. {  
  84.     if (size>1)                                
  85.     {  
  86.         int *left=array;  
  87.         int left_size=size/2;  
  88.         int *right=array+size/2;  
  89.         int right_size=size-left_size;  
  90.   
  91.         mergeSort(left,left_size);  
  92.         mergeSort(right,right_size);  
  93.   
  94.         merge(left,left_size,right,right_size);  
  95.     }  
  96. }  
  97.   
  98.   
  99. void main()  
  100. {  
  101.   
  102.     time_t t_start,t_end;  
  103.     int *array=new int[TXT_NUM_COUNT]; //已经测得有1000001个数据,其中多测一个为空行  
  104.     int count=ImportToArray("largeW.txt",array);  
  105.   
  106.     //printf("%d %d",count,array[count-1]);  
  107.     cout<<count<<" "<<array[count-1]<<endl;  
  108.   
  109.     t_start=time(NULL);  
  110. //  bubbleSort(array,count-1);  
  111.     mergeSort(array,count-1);  
  112.     t_end=time(NULL);  
  113.       
  114. //  cout<<"冒泡用时(S):"<<difftime(t_end,t_start)<<endl;  
  115.   
  116.     cout<<"归并用时(S):"<<difftime(t_end,t_start)<<endl;  
  117.   
  118. //  ExportWithArray("largeW_bubble.txt",array,count-1);  
  119.   
  120.     ExportWithArray("largeW_merge.txt",array,count-1);  
  121.   
  122.     delete [] array;  
  123.   
  124. //  测试用  
  125.     //int a[]={8,2,1,4,6,8,9,0,1,1};  
  126.     bubbleSort(a,10);  
  127.     //mergeSort(a,10);  
  128.     //ExportWithArray("test.txt",a,10);  
  129.     //for(int i=0;i<10;i++)  
  130.     //  cout<<a[i]<<" ";  
  131. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值