排序
1 计数排序 2 #include<iostream> 3 #include<assert.h> 4 using namespace std; 5 //void countSort(int *a, size_t size) 6 //{ 7 // assert(a); 8 // int max = a[0]; 9 // int min = a[0]; 10 // for (size_t i = 1; i < size; i++) 11 // { 12 // if (a[i] < min) 13 // { 14 // min = a[i]; 15 // } 16 // else if (a[i] > max) 17 // { 18 // max = a[i]; 19 // } 20 // } 21 // int range = max - min + 1; 22 // int *countArray = new int[range]; 23 // //memset(countArray, 0, sizeof(int)*range); 24 // for (size_t i = 0; i < range; i++) 25 // { 26 // countArray[i]=0; 27 // } 28 // for (size_t i = 0; i < size; i++) 29 // { 30 // countArray[a[i] - min]++; 31 // } 32 // size_t index = 0; 33 // for (size_t i = 0; i < range; i++) 34 // { 35 // while (countArray[i]--> 0) 36 // { 37 // a[index++] = i + min; 38 // } 39 // } 40 //} 41 void PrintArray(int *a, size_t size) 42 { 43 assert(a); 44 for (size_t i = 0; i<size; i++) 45 { 46 cout<<a[i]<<" "; 47 } 48 cout << endl; 49 } 50 51 基数排序 52 int GetMaxDigit(int *a, size_t size) 53 { 54 int digit = 1; 55 int max = 10; 56 for (size_t i = 0; i < size; i++) 57 { 58 while (a[i] >= max) 59 { 60 ++digit; 61 max *= 10; 62 } 63 } 64 return digit; 65 } 66 void DigitSortLSD(int*a, size_t size) 67 { 68 assert(a); 69 int MaxBit = GetMaxDigit(a, size); 70 int *bucket = new int[size]; 71 int count[10]; int start[10]; 72 int bit = 1; 73 int digit = 1; 74 while (bit<=MaxBit) 75 { 76 memset(count, 0, sizeof(int) * 10); 77 memset(start, 0, sizeof(int) * 10); 78 for (size_t i = 0; i < size; i++) 79 { 80 int num = (a[i] / digit) % 10; 81 count[num]++; 82 } 83 start[0] = 0; 84 for (size_t i = 1; i < size; i++) 85 { 86 start[i] = start[i - 1] + count[i - 1]; 87 88 } 89 for (size_t i = 0; i < size; i++) 90 { 91 int num = (a[i] / digit) % 10; 92 bucket[start[num]++]=a[i]; 93 } 94 memcpy(a, bucket, sizeof(int) * 10); 95 digit *= 10; 96 ++bit; 97 } 98 99 } 100 void main() 101 { 102 int br[] = { 20, 80, 90, 589, 998, 965, 852, 123, 456, 789 }; 103 int len = sizeof(br) / sizeof(int); 104 cout << "原数据如下:" << endl; 105 PrintArray(br, len); 106 cout << "排序后数据如下:" << endl; 107 //countSort(br, len); 108 DigitSortLSD(br, len); 109 PrintArray(br, len); 110 system("pause"); 111 } 112 113 114 115 希尔排序 116 #include<iostream> 117 #include<assert.h> 118 using namespace std; 119 void ShellSort(int *a,size_t size) 120 { 121 assert(a); 122 int gap = size; 123 while (gap > 1) 124 { 125 gap = gap / 3 + 1; 126 for (int i = gap; i < size; ++i) 127 { 128 int index = i; 129 int tmp = a[index]; 130 int end = index - gap; 131 while (end>=0&&a[end] > tmp) 132 { 133 a[end + gap] = a[end]; 134 end -= gap; 135 } 136 a[end + gap] = tmp; 137 } 138 } 139 } 140 void Print(int *a, size_t size) 141 { 142 for (size_t i = 0; i < size; ++i) 143 { 144 cout << a[i] << " "; 145 } 146 cout << endl; 147 } 148 void main() 149 { 150 int a[10] = { 2,5,4,9,3,6,8,7,1,0 }; 151 Print(a, 10); 152 ShellSort(a, 10); 153 Print(a, 10); 154 system("pause"); 155 } 156 157 归并排序 158 159 #include<iostream> 160 #include<assert.h> 161 using namespace std; 162 163 void mergearray(int *a, int first, int mid, int last, int *temp) 164 { 165 int i = first, j = mid + 1; 166 int m = mid, n = last; 167 int k = 0; 168 while (i <= m && j <= n) 169 { 170 if (a[i] <= a[j]) 171 temp[k++] = a[i++]; 172 else 173 temp[k++] = a[j++]; 174 } 175 176 while (i <= m) 177 temp[k++] = a[i++]; 178 179 while (j <= n) 180 temp[k++] = a[j++]; 181 182 for (i = 0; i < k; i++) 183 a[first + i] = temp[i]; 184 } 185 void mergesort(int *a, int first, int last, int *temp) 186 { 187 if (first < last) 188 { 189 int mid = (first + last) / 2; 190 mergesort(a, first, mid, temp); 191 mergesort(a, mid + 1, last, temp); 192 mergearray(a, first, mid, last, temp); 193 } 194 } 195 196 bool MergeSort(int *a, int n) 197 { 198 int *p = new int[n]; 199 if (p == NULL) 200 return false; 201 mergesort(a, 0, n - 1, p); 202 delete[] p; 203 return true; 204 } 205 206 void printArray(int *a, int len) 207 { 208 for (int i = 0; i<len; i++) 209 cout << a[i] << " "; 210 cout << endl; 211 } 212 213 int main() 214 { 215 int a[] = { 49, 38, 65, 97, 76, 13, 27, 49 }; 216 int len = sizeof(a) / sizeof(int); 217 printArray(a, len); 218 MergeSort(a, len); 219 printArray(a, len); 220 system("PAUSE"); 221 return 0; 222 } 223 224 //void mergearray(int *a, int begin1, int end1, int begin2,int end2, int *temp) 225 //{ 226 // assert(a); 227 // int index = begin1; 228 // while (begin1 <= end1&& begin2<= end2) 229 // { 230 // if (a[begin1] <=a[begin2]) 231 // temp[index++] = a[begin1++]; 232 // else 233 // temp[index++] = a[begin2++]; 234 // } 235 // 236 // while (begin1 <= end1) 237 // { 238 // temp[index++] = a[begin1++]; 239 // } 240 // 241 // while (begin2 <= end2) 242 // { 243 // temp[index++] = a[begin2++]; 244 // 245 // } 246 // 247 // for (int i = 0; i < index; i++) 248 // { 249 // a[begin1 + i] = temp[i]; 250 // } 251 // 252 //} 253 //void mergesort(int *a, int first, int last, int *temp) 254 //{ 255 // assert(a); 256 // if (first < last) 257 // { 258 // int mid = first +(last-first) / 2; 259 // mergesort(a, first, mid, temp); 260 // mergesort(a, mid + 1, last, temp); 261 // mergearray(a, first, mid,mid+1, last, temp); 262 // //memcpy(a + first, temp + first, (last - first - 1)*sizeof(int)); 263 // } 264 //} 265 //bool MergeSort(int *a, int n) 266 //{ 267 // int *p = new int[n]; 268 // if (p == NULL) 269 // return false; 270 // mergesort(a, 0, n - 1, p); 271 // delete[] p; 272 // return true; 273 //} 274 //void printArray(int *a, int len) 275 //{ 276 // for (int i = 0; i<len; i++) 277 // cout << a[i] << " "; 278 // cout << endl; 279 //} 280 // 281 //int main() 282 //{ 283 // int a[] = { 49, 38, 65, 97, 76, 13, 27, 49 }; 284 // int len = sizeof(a) / sizeof(int); 285 // printArray(a, len); 286 // MergeSort(a, len); 287 // printArray(a, len); 288 // system("PAUSE"); 289 // return 0; 290 //} 291 292 插入排序 293 294 #include<assert.h> 295 #include<iostream> 296 using namespace std; 297 void InsertSort(int *a, size_t size) 298 { 299 assert(a); 300 for (int i = 1; i < size; ++i) 301 { 302 int index = i; 303 int tmp = a[index]; 304 int end = index - 1; 305 while (end >= 0 && a[end]>tmp) 306 { 307 a[end + 1] = a[end]; 308 --end; 309 } 310 a[end + 1] = tmp; 311 } 312 } 313 void PrintArray(int *a, size_t size) 314 { 315 assert(a); 316 for (int i = 0; i < size; ++i) 317 { 318 cout << a[i] << " "; 319 } 320 cout << endl; 321 } 322 void TestInsertSort() 323 { 324 int a[10] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 }; 325 PrintArray(a, 10); 326 InsertSort(a, 10); 327 PrintArray(a, 10); 328 } 329 330 331 堆排序 332 333 #include<assert.h> 334 #include<iostream> 335 using namespace std; 336 void AdjustDown(int *a,size_t size,int root) 337 { 338 assert(a); 339 int child = 2 * root + 1; 340 while (child < size) 341 { 342 if (child + 1 < size&&a[child + 1] > a[child]) 343 { 344 ++child; 345 } 346 if (a[child]>a[root]) 347 { 348 swap(a[child], a[root]); 349 root = child; 350 child = 2 * root + 1; 351 } 352 else 353 { 354 break; 355 } 356 } 357 } 358 void HeapSort(int *a, size_t size) 359 { 360 assert(a); 361 for (int i = (size-2)/2; i >=0; --i) 362 { 363 AdjustDown(a,size,i); 364 } 365 for (size_t i = size - 1; i > 0; --i) 366 { 367 swap(a[0], a[i]); 368 AdjustDown(a, i, 0); 369 } 370 } 371 void PrintArray(int *a, size_t size) 372 { 373 assert(a); 374 for (int i = 0; i < size; ++i) 375 { 376 cout << a[i] << " "; 377 } 378 cout << endl; 379 } 380 void TestHeapSort() 381 { 382 int a[10] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 }; 383 PrintArray(a, 10); 384 HeapSort(a, 10); 385 PrintArray(a, 10); 386 } 387 int main() 388 { 389 TestHeapSort(); 390 system("pause"); 391 return 0; 392 }