是否有任何方法可以在單鏈表中進行計數排序?我還沒有看到任何例子,如果沒有它們就很難做到。我在數組中有它的例子,並希望在單鏈表中做到這一點。 有人在單鏈表中做過嗎?單鏈表中的計數排序C#
public static int[] CountingSortArray(int[] array)
{
int[] aux = new int[array.Length];
// find the smallest and the largest value
int min = array[0];
int max = array[0];
for (int i = 1; i < array.Length; i++)
{
if (array[i] < min) min = array[i];
else if (array[i] > max) max = array[i];
}
int[] counts = new int[max - min + 1];
for (int i = 0; i < array.Length; i++)
{
counts[array[i] - min]++;
}
counts[0]--;
for (int i = 1; i < counts.Length; i++)
{
counts[i] = counts[i] + counts[i - 1];
}
for (int i = array.Length - 1; i >= 0; i--)
{
aux[counts[array[i] - min]--] = array[i];
}
return aux;
}
請提供您正在尋找的示例。 – STLDeveloper