C# 双重计数排序
我尝试用计数排序法对两位数进行排序。尝试将所有双精度转换为整数,但由于某些原因,什么也没有发生。我对整数进行排序的代码C# 双重计数排序,c#,sorting,C#,Sorting,我尝试用计数排序法对两位数进行排序。尝试将所有双精度转换为整数,但由于某些原因,什么也没有发生。我对整数进行排序的代码 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) {
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;
}
}
}
公共静态void CountingSort(数据数组项){
//O(1)
int max=项目[0];
//O(N)
对于(int i=0;i最大值){
max=项目[i];
}
}
//空间复杂度O(N+K)
int[]计数=新的int[max+1];
//O(N)
对于(int i=0;i0){
而(c>j){
如果(项目[c]==i){
当前=项目[c];
而(c!=j)
{
以前=项目[c-1];
交换项目(c、当前、以前);
c--;
}
}
c--;
}
j++;
计数[i]-;
c=项目。长度-1;
}
}
}
甚至可以使用计数排序对双位数进行排序吗?要对双位数数组进行排序,请使用
double[] doubleArray = new double[5] { 8.1, 10.2, 2.5, 6.7, 3.3 };
doubleArray = Array.Sort(doubleArray);
之后,使用循环进行计数