简体   繁体   中英

Counting Sort: count array minimum, maximum and frequency

I want to modify counting sort to efficiently cater for a range of values where the minimum value is not 0. My problems with the code is finding the minimum value if it is not 0, the minimum value is supposed to be for example, if the range of the list is 100 000 - 110 000, the min is 100 000. But the frequency (count) of the count array CANNOT be 100 001 My code is currently not working or sorting at all for a list of 20 000 integers and number ranges from 1000 - 9999.

It works when min is 0 but that is not how counting sort is meant to be implemented efficiently...

public static  int findMinValue(int[] List)
        {

            int min;
            min = List[0];
            return min;
        }

        static void countingsort(int[] List, int max)
        /* pre:  List has .Length integers in it. The maximum value in the list is max.
        * post: Using the countingsort algorithm, sort the list of integers into ascending order. */
        {

            // ADD CODE FOR METHOD countingsort BELOW
            Stopwatch timer = new Stopwatch();
                timer.Start();
            int[] countArray = new int[max + 1];
            int min = findMinValue(countArray);
            for (int i = 0; i <= max; i++)
            {
                countArray[i] = 0;

            }
            for (int x = 0; x < List.Length; x++)
            {
                countArray[List[x]] = countArray[List[x]] + min;
                // 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++
                }
            }
            Display(List);
            DisplayCount(countArray);
            timer.Stop();

            Console.WriteLine("Time Taken for Basic Selection Sort is {0} milliseconds", timer.ElapsedMilliseconds);



        }
        public static void DisplayCount(int[] Array)
        {
            int count = 0;
            for (int i = 0; i < Array.Length; i++)
            {
                count++;
            }
            Console.WriteLine("Elements in count array is {0} ", count);
        }

The list does not get sorted and it displays elements in count array as 10 000.

Here are the things I noticed:

  1. The minimum value finding function takes the first value from the array, rather than the actual minimum. you might want to use List.Min(); .

  2. In countArray declaration, you should use the difference between maximum and minimum values + 1 as the size.

  3. You need to offset the index while accessing the countArray with found minimum value, since the 0 index element should correspond to the minimum value rather than 0.

  4. While iterating over the countArray in for (int i = 0; i <= max; i++) , you should not use the max value in the condition. Unless the minimum is 0 it will differ from array size.

  5. In the count displaying function, you may simply use the Array.Length , there is no need to iterate over the array.

I will not provide you with a code for the algorithm itself since it is obviously an Uni/College/HS assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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