重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
void CountSort(int* a, int n) { assert(a); int minValue = a[0]; int maxValue = a[0]; for (int i = 0; i < n; ++i) { if (minValue > a[i]) { minValue = a[i]; } if (maxValue < a[i]) { maxValue = a[i]; } } int range = maxValue-minValue+1; int* tmp = new int[range]; memset(tmp, 0, sizeof(int)*range); int index = 0; while (index < n) { tmp[a[index]-minValue]++; ++index; } index = 0; for (int i = 0; i < range; ++i) { while (tmp[i]--) { a[index++] = i+minValue; } } delete[] tmp; }