0
我想排序與計數排序雙位數。試圖將所有雙打轉換爲整數,但由於某種原因沒有任何反應。我的代碼整理了整數。排序與計數排序
public static void CountingSort(DataArray items) {
// O(1)
int max = items[0];
// O(N)
for (int i = 0; i < items.Length; i++) {
if (items[i] > max) {
max = items[i];
}
}
// Space complexity O(N+K)
int[] counts = new int[max + 1];
// O(N)
for (int i = 0; i < items.Length; i++) {
counts[items[i]]++;
}
// O(N+K)
int j = 0;
int c = items.Length - 1;
int current, previous;
for (int i = 0; i < counts.Length; i++) {
while (counts[i] > 0) {
while (c > j) {
if (items[c] == i) {
current = items[c];
while (c != j)
{
previous = items[c - 1];
items.Swap(c, current, previous);
c--;
}
}
c--;
}
j++;
counts[i]--;
c = items.Length - 1;
}
}
}
是否有可能通過計數排序來排序雙數位?