简体   繁体   English

计算从升序到降序的排序方法

[英]Counting Sort Method from Ascending to Descending order

I coded this method thinking it is in descending order but the output displays it as ascending order. 我编码这个方法认为它是降序但输出显示为升序。 How would I change my method to display my list in descending order? 如何更改我的方法以降序显示列表?

I tried to use a FindMinValue method instead of the max value but it gives an out of bounds error. 我试图使用FindMinValue方法而不是最大值,但它给出了一个越界错误。

  static int findMaxValue(int[] List)
    {
        int max = -1;
        for (int i = 0; i < List.Length; i++)
        {
            max = Math.Max(max, List[i]);
        }
        return max;
    }
    public static int findMinValue(int[] List)
    {
        //go through list to find min

        int min = int.MaxValue;
        for (int i = 0; i < List.Length; i++)
        {
            min = Math.Min(min, List[i]);
        }
        min = List[0];
        return min;
    }

    static void countingsort(int[] List, int max)

        // ADD CODE FOR METHOD countingsort BELOW
        Stopwatch timer = new Stopwatch();
            timer.Start();

        int[] countArray = new int[max + 1];
        for (int i = 0; i <= max; i++)
        {
            countArray[i] = 0;

        }
        for (int x = 0; x < List.Length; x++)
        {
            countArray[List[x]] = countArray[List[x]] + 1;
            // or countArray[List[x]]++;


        }
        int index = 0;
        for (int y = 0; y <= max; y++)
        {
            //List[y] = countArray[y];
            for (int j = 0; j < countArray[y]; j++)
            {
                List[index] = y;
                index = index + 1;
                // or index++
            }
        }
        timer.Stop();
        Display(List);
    }

Give frequent items less weight by counting down : Change the line 给频繁项重量倒计时 :更改行

countArray[List[x]] = countArray[List[x]] + 1;

to

countArray[List[x]] = countArray[List[x]] - 1;

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

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