我正在尝试使用计数排序对两位数进行排序。尝试将所有双精度数转换为整数,但由于某种原因什么也没有发生。我对整型数进行排序的代码。
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;
}
}
}是否有可能使用计数排序来对两位数进行排序?
发布于 2017-04-05 12:27:13
要对double数组进行排序,请使用
double[] doubleArray = new double[5] { 8.1, 10.2, 2.5, 6.7, 3.3 };
doubleArray = Array.Sort(doubleArray);在此之后,使用循环进行计数。
https://stackoverflow.com/questions/43220706
复制相似问题