Search code examples
c#sortingbucket-sort

c# implementing a bucket sort algorithm


Good evening everyone here! I created a bucket sort algorithm, but it throws me an error that index is out of range. Could you please tell me where is the problem? I can't find the solution by myself, that's why I'm asking for your help

public int[] Sort(int[] unsortedSequence)
        {
            List<List<int>> buckets = new List<List<int>>();
            InitializeBuckets(buckets);
            Scatter(unsortedSequence, buckets);
            int i = 0;
            foreach (List<int> bucket in buckets)
            {
                int[] arr = bucket.ToArray();
                InsertionSort(arr);
                foreach (int d in arr)
                {
                    unsortedSequence[i++] = d;
                }
            }
            return unsortedSequence;
        }
        private static void Scatter(int[] array, List<List<int>> buckets)
        {
            foreach (int value in array)
            {
                int bucketNumber = GetBucketNumber(value);
                buckets[bucketNumber].Add(value);
            }
        }
        private static void InsertionSort(int[] array)
        {
            int j;
            int temp;

            for (int i = 1; i < array.Length; i++)
            {
                j = i;
                while (j > 0 && array[j] < array[j - 1])
                {
                    temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                    j--;
                }
            }
        }
        private static int GetBucketNumber(int value)
        {
            int val = value * 10;

            return val;
        }

        private static void InitializeBuckets(List<List<int>> buckets)
        {
            for (int i = 0; i < 10; i++)
            {
                List<int> a = new List<int>();
                buckets.Add(a);
            }
        }

Solution

  • I am not sure what logic you did for sorting this but I got why you are getting index out of range error.

    Error in Code

    You are creating 10 buckets and now you are trying to generate bucket number by multiplying current value or array with 10.

    For example, if your current value of array is 2 then generated bucket number will be 20. You got only 10 buckets so Scatter() method will give you error.

     private static void Scatter(int[] array, List<List<int>> buckets)
     {
         foreach (int value in array)
         {
             int bucketNumber = GetBucketNumber(value);
             buckets[bucketNumber].Add(value); // ERROR HERE
         }
     }
    

    SOLUTION

    Actually, there is problem with GetBucketNumber() method. You should use remainder not multiplication. Change method with following.

    private static int GetBucketNumber(int value)
    {
        int val = value % 10;
        return val;
    }
    

    You must do

    Try to solve your problem with hard attempts before you ask for help. Run your program on paper first I mean confirm your logic before you start coding. Have faith in you and give enough time to your attempts. Enjoy coding.