要求对数组a进行排序,要求时间复杂度为O(N)

xiaoxiao2021-02-27  218

题目: int a[] = {12,13,12,13,19,18,15,12,15,16,17},要求对数组a进行排序,要求时间复杂度为O(N) 。

void sort(int *arr, int size) { if (arr == NULL || size <= 0) exit(1); int data[20]; memset(data, 0, SIZE*sizeof(int)); int i = 0; for (i = 0; i < size; ++i) { data[arr[i]] += 1; } i = 0; for (int j = 0; j < SIZE; j++) { if (data[j]==1) { arr[i++] = j; } else if (data[j]>1) { arr[i++] = j; data[j] -= 1; j--; } else ; } }
转载请注明原文地址: https://www.6miu.com/read-11386.html

最新回复(0)