。。几种排序的时间比较

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<time.h>
  4 #include<algorithm>
  5 using namespace std;
  6 
  7 int n = 50000;
  8 
  9 void select(int b[])
 10 {
 11     for (int i = 1; i <n; i++)
 12         for (int j = i + 1; j <= n; j++)
 13             if (b[i] > b[j]) swap(b[i], b[j]);
 14 } 
 15 
 16 void bubble(int b[])
 17 {
 18     for (int i = 1; i < n; i ++)
 19         for (int j = 1; j < n; j ++)
 20             if (b[j] > b[j + 1]) swap(b[j], b[j + 1]);    
 21 }
 22 
 23 void quick(int b[], int l, int r)
 24 {
 25     int i = l, j = r, x, y;
 26     x = b[(l + r) / 2];
 27     while (i <= j)
 28     {
 29         while (b[i] < x) i++;
 30         while (x < b[j]) j--;
 31         if (i <= j)
 32         {
 33             swap(b[i], b[j]);
 34             i++; j--;
 35         }
 36     }
 37     if (i < r) quick(b, i, r);
 38     if (l < j) quick(b, l, j);
 39 }
 40 
 41 int c[1000005];
 42 void merge(int b[], int l, int r, int mid)
 43 {
 44     for (int i = l; i <= r; i++) c[i] = b[i];
 45     int x1 = l, x2 = mid + 1;
 46     for (int i = l; i <= r; i++)
 47     {
 48         if (x1 > mid) b[i] = c[x2++];
 49         else if (x2 > r) b[i] = c[x1++];
 50         else if (c[x1] < c[x2]) b[i] = c[x1++];
 51         else b[i] = c[x2++];
 52     }
 53 }
 54 void mergesort(int b[], int l, int r)
 55 {
 56     int mid = (l + r) / 2;
 57     if (l >= r) return;
 58     mergesort(b, l, mid);
 59     mergesort(b, mid + 1, r);
 60     merge(b, l, r, mid);
 61 }
 62 
 63 void insert(int b[])
 64 {
 65     for (int i = 2; i <= n; i++)
 66     {
 67         int j = i;
 68         while (j > 1 && b[j] < b[j - 1]) 
 69         {
 70             swap(b[j], b[j - 1]);
 71             j--;
 72         }
 73     }
 74 }
 75 
 76 void heap(int b[], int i, int size)
 77 {
 78     int child;
 79     for (; 2 * i <= size; i = child)
 80     {
 81         child = 2 * i;
 82         if (child < size && b[child + 1] > b[child]) child++;
 83         if (b[i] < b[child]) swap(b[i], b[child]);
 84         else break;
 85     }
 86 }
 87 void heapsort(int b[])
 88 {
 89     for (int i = n / 2; i >= 1; i--)
 90         heap(b, i, n);
 91     for (int i = n; i > 1; i--)
 92     {
 93         swap(b[i], b[1]);
 94         heap(b, 1, i - 1);
 95     }
 96 }
 97 
 98 int a[1000005], b[1000005];
 99 int main()
100 {
101     srand(time(0));
102     for (int i = 1; i <= n; i++) a[i] = rand();
103     clock_t begin, duration;
104      
105     for (int i = 1; i <= n; i++) b[i] = a[i];
106     begin = clock();
107     select(b);
108     duration = clock() - begin;
109 //    for (int i = 1; i <= n; i++) printf("%d ",b[i]); printf("\n");
110     printf("select sort = %dms\n\n",duration*1000/CLOCKS_PER_SEC);
111     
112     for (int i = 1; i <= n; i++) b[i] = a[i];
113     begin = clock();
114     bubble(b);
115     duration = clock() - begin;
116 //    for (int i = 1; i <= n; i++) printf("%d ",b[i]); printf("\n");
117     printf("bubble sort = %dms\n\n",duration*1000/CLOCKS_PER_SEC);
118     
119     for (int i = 1; i <= n; i++) b[i] = a[i];
120     begin = clock();
121     quick(b, 1, n);
122     duration = clock() - begin;
123 //    for (int i = 1; i <= n; i++) printf("%d ",b[i]); printf("\n");
124     printf("quick sort = %dms\n\n",duration*1000/CLOCKS_PER_SEC);
125     
126     for (int i = 1; i <= n; i++) b[i] = a[i];
127     begin = clock();
128     mergesort(b, 1, n);
129     duration = clock() - begin;
130 //    for (int i = 1; i <= n; i++) printf("%d ",b[i]); printf("\n");
131     printf("merge sort = %dms\n\n",duration*1000/CLOCKS_PER_SEC);
132     
133     for (int i = 1; i <= n; i++) b[i] = a[i];
134     begin = clock();
135     insert(b);
136     duration = clock() - begin;
137 //    for (int i = 1; i <= n; i++) printf("%d ",b[i]); printf("\n");
138     printf("insert sort = %dms\n\n",duration*1000/CLOCKS_PER_SEC);
139     
140     for (int i = 1; i <= n; i++) b[i] = a[i];
141     begin = clock();
142     heapsort(b);
143     duration = clock() - begin;
144 //    for (int i = 1; i <= n; i++) printf("%d ",b[i]); printf("\n");
145     printf("heap sort = %dms\n\n",duration*1000/CLOCKS_PER_SEC);
146 }
View Code

 

posted on 2016-06-02 13:06  xiao_xin  阅读(356)  评论(0编辑  收藏  举报

导航