1 #include <bits/stdc++.h>
 2 //#include <time.h>
 3 //#include <stdlib.h>
 4 using namespace std;
 5 
 6 template<typename var>
 7 void bubbleSort(var array[],int len)
 8 {    //简单冒泡排序 
 9     for(int i=1;i<len;i++)
10         for(int j=0;j<len-i;j++)
11             if(array[j]>array[j+1])
12                 swap(array[j],array[j+1]);
13 }
14 template<typename var>
15 void selectSort(var array[],int len)
16 {    //简单选择排序
17     for(int i=0;i<len-1;i++)
18         for(int j=i+1;j<len;j++)
19              if(array[j]<array[i])
20                  swap(array[i],array[j]);
21 }
22 template<typename var>
23 void insertSort(var array[],int len)
24 {    //简单插入排序
25     for(int i=1;i<len;i++)
26     {
27         for(int j=i;j>0&&array[j]<array[j-1];j--)
28             swap(array[j],array[j-1]);
29     }    
30 }
31 int main()
32 {
33     double array[1005];
34     srand(time(0));
35     cout<<"生成100个0~1000随机数\n";
36     for(int i=0;i<1000;i++)
37     {
38         array[i]=10000*((double)rand()/RAND_MAX);
39         cout<<setw(10)<<array[i];
40     }
41     clock_t ts=clock();
42     insertSort(array,1000);
43     clock_t te=clock();
44     cout<<"\n排序后的1000个数\n";
45     cout<<"花费时间:"<<(1000.0*(te-ts)/CLOCKS_PER_SEC)<<"ms"<<endl; 
46     for(int i=0;i<1000;i++)
47         cout<<setw(10)<<array[i];
48     return 0;
49 }

 

 posted on 2017-10-20 12:33  theFresh  阅读(274)  评论(0编辑  收藏  举报