首页 > 代码库 > 产生随机数,测试快排

产生随机数,测试快排

  1 #include<iostream>  2 #include<cstdlib>  3 #include<ctime>  4 #include<fstream>  5 #include<algorithm>  6 using namespace std;  7 int main()  8 {  9     srand(unsigned(time(0))); 10     freopen("random1.txt","w",stdout); 11     for(int i = 0 ; i < 1000 ; i++) 12     { 13         int a = rand() % 100; 14         cout<< a <<" "; 15     } 16     fclose(stdout); 17  18 } 19 #include<iostream> 20 #include<cstdlib> 21 #include<ctime> 22 #include<fstream> 23 #include<algorithm> 24 using namespace std; 25 int fuzhukuaipai(int a[],int i,int j) 26 { 27      int key = a[i]; 28      while(i<j){ 29  30      while(a[j]>=key&&i<j)--j; 31      a[i] = a[j]; 32   while(a[i]<=key&&i<j)++i; 33     a[j] = a[i]; 34     } 35     a[i] = key; 36     return i; 37 } 38 void kuaipai(int a[],int i,int j) 39 { 40         if(i<j) 41         { 42             int p = fuzhukuaipai(a,i,j); 43             kuaipai(a,i,p-1); 44             kuaipai(a,p+1,j); 45         } 46 } 47 int Partition(int a[],int p , int r) 48 { 49     int x = a[r]; 50     int i = p-1; 51     for(int j = p ; j < r ;j++ ) 52     { 53         if(a[j]<=x) 54         { 55             i ++; 56             swap(a[i],a[j]); 57         } 58     } 59     swap (a[i+1],a[r]); 60     return i+1; 61 } 62 void QuickSort(int a[],int p,int r) 63 { 64     if(p<r) 65     { 66         int q=Partition(a,p,r); 67         QuickSort(a,p,q-1); 68         QuickSort(a,q+1,r); 69     } 70 } 71 void maopao(int a[],int n) 72 { 73     for(int i = 0 ; i < n ; i ++) 74     { 75         for(int j = i ; j < n ; j++) 76         { 77             if(a[i]>a[j])swap(a[i],a[j]); 78         } 79     } 80 } 81 int main() 82 { 83     int a[100000] ; 84     ifstream fin; 85     fin.open("random_number.txt"); 86     for(int i = 0 ; i < 100000 ; i++) 87     { 88         fin>>a[i]; 89     } 90     fin.close(); 91     time_t Start; 92     time_t End; 93     Start = clock(); 94    // QuickSort(a,0,100000); 95     kuaipai(a,0,100000); 96     End = clock(); 97     cout<<"kuaipai-spend:"<<End-Start<<endl; 98 //    ofstream fout; 99 //    fout.open("aftersort.txt");100 //    for(int i = 0 ; i < 100000 ; i ++)101 //    {102 //            fout<<a[i]<<"\t";103 //    }104 fin.open("random_number.txt");105     for(int i = 0 ; i < 100000 ; i++)106     {107         fin>>a[i];108     }109     fin.close();110     Start = clock();111     QuickSort(a,0,100000);112     End = clock();113     cout<<"QuickSort-spend:"<<End-Start<<endl;114 115 116     fin.open("random_number.txt");117     for(int i = 0 ; i < 100000 ; i++)118     {119         fin>>a[i];120     }121     fin.close();122     Start = clock();123     sort(a,a+100000);124     End = clock();125     cout<<"Sort-spend:"<<End-Start<<endl;126 127 }

 

产生随机数,测试快排