【问题标题】:sorting numbers array question排序数字数组问题
【发布时间】:2011-03-09 22:24:43
【问题描述】:

我在 c# 中遇到了数组问题。例如,我们有一个存储索引数组(数组长度 0-99),一个随机生成数字数组(数组长度 0-99)和频率数组(重复多少次数字)。

例子

i: 0 1 2 3 4 ... i - 索引
n: 5 2 1 2 0 ... n - 数字
f: 1 1 2 1 0 ... f - 频率

它是计数排序的一部分。下面我们有另一个例子我想如何排序 无需计算

我:0 1 2 3 4 ...
n: 5 2 1 2 0 ...
f: 1 1 2 1 0 ...
s: 0 1 2 2 3 ... s - 已排序

-> 频率告诉我们有多少个 0,1,... 我们只写下来

int[] arr = new int[100]; //generated numbers
int[] arr2 = new int[100]; //sorted array
int[] counter = new int[100]; //frequencies

//frequencies
for (int i = 0; i < st_el; i++) 
{
    counter[arr[i]] += 1;
}

for(int i=0; i<arr.length; i++)
{
    for(int j=0; j<arr.length; j++)
    {
        //I do not know how to implement?
    }
}

【问题讨论】:

  • 我不太明白问题是什么,你能澄清一下你想以什么方式排序哪个数组吗?
  • 您希望排序后的数组arr2 成为按引用频率排列的商店编号吗?但是您示例中的 s 数组根本没有意义。请解释您要做什么。

标签: c# arrays sorting


【解决方案1】:

使用 LINQ,您可以执行以下操作:

var frequencies = numbers.GroupBy(n => n)
                         .Select(g => new { Number = g.Key, Frequency = g.Count() })
                         .ToList();
foreach (var item in frequencies)
{
    Console.WriteLine("Number {0} occurs {1} times", item.Number, item.Frequency);
}

这将为您提供 numbers 数组中每个数字的频率 - 我认为这就是您想要的。

编辑:

我想我现在明白了:您自己决定 counting sort 部分 - 假设您只允许 0 到 99 之间的随机数,您只需检查计数数组的每个元素即可检查特定数字的出现次数,然后重复该数字多次(未经测试):

int index = 0;
for(int i=0; i< counter.length; i++)
{
  for(j=index;j<index+counter[i];j++)
     arr2[j] = i;
  index+=counter[i];
}

【讨论】:

  • 感谢您的回答,但不幸的是,这不是我想要的。我忘了写我需要算法而不需要任何其他方法或其他任何东西。
  • @Strausa:检查我的编辑,这可能足以让您开始并验证自己 - 我认为这是家庭作业
  • 是的,这是我的家庭作业的一部分,但我坐在这个任务前面没有任何成功。我将尝试自己完成这项工作以进行培训。感谢您的建议
【解决方案2】:

我根本不会使用数组。我几乎从不使用它们,尤其是考虑到(我假设)你对数组长度的人为限制。

假设您至少使用 .Net 3.5,以下是我的处理方式:

class Program
{
    static void Main(string[] args)
    {
        var sorted = new List<int>();
        var frequencies = new Dictionary<int, int>();

        //Get a bunch of random numbers
        var numbers = GetSomeRandomNumbers(100);

        //Sort the numbers asscenting
        foreach (var number in numbers.OrderBy(i => i))
        {
            //This will add the numbers in the expected order
            sorted.Add(number);

            //Frequencies is just a lookup table of number -> frequency
            if (frequencies.ContainsKey(number))
                frequencies[number]++;
            else
                frequencies.Add(number, 1);
        }

        Console.WriteLine("--SORTED--");
        sorted.ForEach(number => Console.WriteLine(number));

        Console.WriteLine("--FREQUENCIES--");
        //Dump all of the frequencies as a quick test
        frequencies.ToList().ForEach(pair => Console.WriteLine(string.Format("{0} occurrences of {1}", pair.Value, pair.Key)));

        //Wiat
        Console.ReadLine();
    }

    private static List<int> GetSomeRandomNumbers(int count)
    {
        var random = new Random();
        var result = new List<int>();

        for (int i = 0; i < count; i++)
            result.Add(random.Next(0, 100));

        return result;
    }
}

【讨论】:

  • 你会看到你的随机数在你的实现中不会那么随机
  • 谢谢布兰登,我会把它写下来做练习。
猜你喜欢
  • 2011-09-27
  • 2018-12-28
  • 1970-01-01
  • 2022-06-21
  • 1970-01-01
  • 2016-09-26
  • 2013-05-29
  • 1970-01-01
相关资源
最近更新 更多