Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
            }
        }
like image 768
finsters Avatar asked Nov 28 '25 19:11

finsters


2 Answers

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.

like image 115
Gaurang Dave Avatar answered Dec 01 '25 09:12

Gaurang Dave


Before I answer, I want to strongly suggest that you always make a good faith attempt at solving a problem first before seeking outside help. I don't necessarily think you didn't try, but this problem was easily identified by stepping through the debugger.

If that's an aspect of coding you're not too familiar with, I strongly recommend making it a priority to learn - it will only make you a better developer in the long run.

The problem is occurring in the Scatter method at this point:

int bucketNumber = GetBucketNumber(value); buckets[bucketNumber].Add(value);

The exception occurs because GetBucketNumber multiplies the input by 10, but you're using that multiplied value as the index for buckets.

like image 40
sbsd Avatar answered Dec 01 '25 08:12

sbsd