简体   繁体   English

排序数字数组问题

[英]sorting numbers array question

I have a problem with arrays in c#. 我在C#中遇到数组问题。 for example we have one array for store indexes (array length 0-99 ), one array for random generated numbers (array length 0-99) and array for frequencies (how many time numbers repeat). 例如,我们有一个用于存储索引的数组(数组长度为0-99),一个数组用于随机生成的数字(数组长度为0-99),以及一个频率数组(用于重复多少个时间数字)。

Example

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

it is part of counting sort. 这是计数排序的一部分。 And below we have another one example how I would like to sort without comulative computing 下面是另一个示例,我想如何在不进行通信计算的情况下进行排序

i: 0 1 2 3 4 ... i: 0 1 2 3 4 ...
n: 5 2 1 2 0 ... n:5 2 1 2 0 ...
f: 1 1 2 1 0 ... f: 1 1 2 1 0 ...
s: 0 1 2 2 3 ... s - sorted s:0 1 2 2 3 ... s-排序

-> frequency tell us how many 0,1,... are there and we only write it down ->频率告诉我们那里有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?
    }
}

using LINQ you could do something like this: 使用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);
}

This will give you the frequency of each number in the numbers array - I think that's what you want. 这将为您提供数字数组中每个数字的频率-我这就是您想要的。

Edit: 编辑:

I think I understand now: You're up to the counting sort part itself - given the example you have only random numbers between 0 and 99 would be allowed, you only have to check every element of the count array to check the number of occurrences of the specific number, then repeat that number that many times (untested): 我想我现在明白了:您可以自己完成计数排序部分-给出的示例仅允许使用0到99之间的随机数,您只需要检查count数组的每个元素即可检查出现次数特定号码的号码,然后重复该号码多次(未经测试):

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];
}

I wouldn't use arrays at all. 我根本不会使用数组。 I hardly ever use them, especially given the (I assume) artificial limitations you have on array length. 我几乎从未使用过它们,尤其是考虑到您对阵列长度的人为限制(我假设)。

Here is how I would handle it, asuming you are using at least .Net 3.5: 假设您至少使用.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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM