这个排序问题的时间复杂度是多少?
What is the time complexity of this sorting problem?
问题给出了一个大小为 n 的数组,其值范围为 [1-100]。创建一个排序算法并讨论它的时间、space 和最优性。我对渐近分析没有很好的理解,不知道如何回答这个问题。
算法:
void sort(int a[], int n) {
int temp[100] = {0};
for(int i=0; i<n; i++)
temp[a[i]-1]++;
int c = 0;
for(int i=0; i<100; i++)
for(int j=0; j<temp[i]; j++) {
a[c] = i+1;
c++;
}
}
是O(n)
,请参考Count Sort
问题给出了一个大小为 n 的数组,其值范围为 [1-100]。创建一个排序算法并讨论它的时间、space 和最优性。我对渐近分析没有很好的理解,不知道如何回答这个问题。
算法:
void sort(int a[], int n) {
int temp[100] = {0};
for(int i=0; i<n; i++)
temp[a[i]-1]++;
int c = 0;
for(int i=0; i<100; i++)
for(int j=0; j<temp[i]; j++) {
a[c] = i+1;
c++;
}
}
是O(n)
,请参考Count Sort