1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| #include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100000
void RandCreate(int *a){ int i; for ( i = 0; i < N; ++i) a[i] = 1 + (rand()%1000); }
void Swap(int *a,int i,int j){ int tmp; tmp = a[i]; a[i] = a[j]; a[j] = tmp; }
void SelectSort(int *l){ int minx,i,j; for (i= 0; i < N-1; ++i){ minx = i; for (j = i+1; j < N; ++j) if( l[minx] > l[j] ) minx = j; if( minx != i) Swap(l,minx,i); } }
void InsertSort(int *l){ int i,j; for(i = 1;i < N;i ++){ int insertItem = l[i]; for(j = i-1;j >= 0;j --){ if(insertItem < l[j]) l[j+1] = l[j]; else break; } l[j+1] = insertItem; } }
void BubbleSort(int *l){ int i,j; for(i = N-1;i > 0;i --){ int isSwap = 0; for(j = 0; j<i;j ++){ if(l[j] > l[j+1]){ Swap(l,j,j+1); isSwap = 1; } } if(!isSwap) break; } }
int Partition(int *l,int low,int high){ int i = low,j = high + 1; int pivot = l[low]; do{ do i++; while(l[i] < pivot); do j--; while(l[j] > pivot); if(i < j) Swap(l,i,j); }while(i < j); Swap(l,low,j); return j; }
void QuickSort(int *l,int low,int high){ int k; if(low < high){ k = Partition(l,low,high); QuickSort(l,low,k-1); QuickSort(l,k+1,high); } }
void QSort(int *l){ QuickSort(l,0,N-1); }
void Merge(int *l,int Temp[],int i1,int j1,int i2,int j2,int *k){ int i = i1,j = i2; while((i <= j1)&&(j<=j2)){ if(l[i] <= l[j]){ Temp[(*k)++] = l[i++]; } else Temp[(*k)++] = l[j++]; } while(i <= j1) Temp[(*k)++] = l[i++]; while(j <= j2) Temp[(*k)++] = l[j++]; }
void MergeSort(int *l){ int Temp[N]; int i1,j1,i2,j2,i,k,size = 1; while(size < N){ i1 = 0; k = 0; while(i1+size < N){ i2 = i1+size; j1 = i2-1; if(i2+size-1 > N-1){ j2 = N-1; } else j2 = i2+size-1; Merge(l,Temp,i1,j1,i2,j2,&k); i1 = j2+1; } for(i = 0;i < k;i ++){ l[i] = Temp[i]; } size *= 2; } }
typedef struct heap{ int n; int *data; }heap;
void AdjustHeap(int Heap[],int s,int m){ int temp = Heap[s]; for(int j = 2*s+1;j <= m; j *= 2){ if(j < m &&Heap[j] < Heap[j+1]) j++; if(temp > Heap[j]) break; Heap[s] = Heap[j]; s = j; } Heap[s] = temp; }
void CreateHeap(int *heap,int n){ int i; for(i = (n-2)/2;i >= 0;i --) AdjustHeap(heap,i,n); }
void heapInit(heap *hp,int *a,int n){ hp->n = n; hp->data = (int *)malloc( sizeof(int) *n); int i; for( i = 0;i < n;i ++) hp->data[i] = a[i]; CreateHeap(hp->data ,N-1); }
void HeapSort(heap *hp){ int i; for( i=hp->n/2 ; i>0 ;i--) AdjustHeap(hp->data,i,hp->n); for( i = hp-> n-1 ;i>0;i--){ Swap( hp->data,0,i); AdjustHeap(hp->data,0,i-1); } }
int main(){ srand(time( NULL )); int a[6][N]; int i,j; RandCreate(a[0]); for (int i = 1; i < 6; ++i) for (int j = 0; j < N; ++j) a[i][j] = a[0][j];
double start1 = (double) clock(); SelectSort(a[0]); double end1 = (double) clock(); double diff1 = difftime(end1,start1); printf("%18s%10lf\n","简单选择排序时间:",diff1);
double start2 = (double) clock(); InsertSort(a[1]); double end2 = (double) clock(); double diff2 = difftime(end2,start2); printf("%18s%10lf\n","直接插入排序时间:",diff2);
double start3 = (double) clock(); BubbleSort(a[2]); double end3 = (double) clock(); double diff3 = difftime(end3,start3); printf("%18s%10lf\n","冒泡排序时间:",diff3);
double start5 = (double) clock(); MergeSort(a[4]); double end5 = (double) clock(); double diff5 = difftime(end5,start5); printf("%18s%10lf\n","两路排序时间:",diff5);
double start4 = (double) clock(); QSort(a[3]); double end4 = (double) clock(); double diff4 = difftime(end4,start4); printf("%18s%10lf\n","快速排序时间:",diff4);
heap hp; heapInit(&hp,a[5],N); double start6 = (double) clock(); HeapSort(&hp); double end6 = (double) clock(); double diff6 = difftime(end6,start6); printf("%18s%10lf\n","堆排序时间:",diff6);
system("pause"); return 0; }
|