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
| // 计数排序.cpp: 定义控制台应用程序的入口点。 //
#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std;
#define MAX_SIZE 10
void CountintSort(int a[], int size) { int count[MAX_SIZE + 1] = { 0 }; for (int i = 0; i < MAX_SIZE; i++) { count[a[i]] = count[a[i]] + 1; } int index = 0;
for (int i = 0; i <= MAX_SIZE; i++) { for (int j = 1; j <= count[i]; j++) { a[index] = i; index += 1; } } } int main() { srand(time(0)); int a[MAX_SIZE]; for (int i = 0; i < MAX_SIZE; i++) { a[i] = rand() % MAX_SIZE + 1; } for (int i = 0; i < MAX_SIZE; i++) { cout << a[i] << " "; } cout << endl; CountintSort(a, MAX_SIZE); for (int i = 0; i < MAX_SIZE; i++) { cout << a[i] << " "; } cout << endl; return 0; }
|