首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Counting排序进行双精度排序

使用Counting排序进行双精度排序
EN

Stack Overflow用户
提问于 2017-04-05 10:18:05
回答 1查看 688关注 0票数 0

我正在尝试使用计数排序对两位数进行排序。尝试将所有双精度数转换为整数,但由于某种原因什么也没有发生。我对整型数进行排序的代码。

代码语言:javascript
复制
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;
    }
    }
}

是否有可能使用计数排序来对两位数进行排序?

EN

回答 1

Stack Overflow用户

发布于 2017-04-05 12:27:13

要对double数组进行排序,请使用

代码语言:javascript
复制
double[] doubleArray = new double[5] { 8.1, 10.2, 2.5, 6.7, 3.3 };
doubleArray  = Array.Sort(doubleArray);

在此之后,使用循环进行计数。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43220706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档