Bucket sort with huge random numbers

1.5k Views Asked by At

I know Bucket sort is has a lot of examples everywhere, so I tried to implement this so it can take huge random numbers with no luck

void Bucket_sort(int arr[], int max){
     const int maxsize = max;
     int bucket_list = new int [maxsize+1];
     int length = sozeof(bucket_list) / sizeof(bucket[0]);
     for(int i = 0; i <max;i++){
         bucket_list[i] = 0; //fill with zeros
       }
       for (unsigned int i = 0; i <length; i++){
           bucket_list[arr[i]]++;
        }
        int position = 0;
        for (unsigned int i = 0 i < length; i++){
           for(int k = 0; k<bucket_list[i];k++){
                arr[position++] = i;
             }
          }
       }
      int main() {
         int max = 50000
         int arr[max];
         for (int i = 0; i < max; i++){
            arr[i] = rand() % 50000;
            }
            cout<<"Here are the numbers before Bucker Sort"<<endl;
            for (int j = 0; j < max; j++){
                 cout<<arr[j];
              }
            Bucket_sort(arr,max);
            for (int k = 0; k<max; k++){
               cout<<arr[k];
              }
           }

some how I can't get it working, it will just out put the same order (unsorted) I did find some same questions as mine, but none of them helped, here is one https://stackoverflow.com/questions/20037176/c-bucket-sort-putting-integers-into-buckets

2

There are 2 best solutions below

3
The Dark On

This line:

bucket_list = 0; //fill with zeros

this is changing your pointer, not filling with zeros. You should use

bucket_list[i] = 0; //fill with zeros

Edit: There are a lot more compiler issues with your code. Once you have those sorted out, the calculation of length is still wrong. You can't use the sizeof dividing trick, because bucket_list isn't an array. Replace

int length = sozeof(bucket_list) / sizeof(bucket[0]);

with

int length = maxsize

or just don't use length at all (you already have maxsize).

0
Surendra Kumar On
#include<iostream>
#include<conio.h>
#include<stdlib.h>

using namespace std;

void Bucket_sort(int arr[], int max){

     int maxsize = max;
     int *bucket_list = new int[maxsize+1];
    // int length = sozeof(bucket_list) / sizeof(bucket[0]);
     int length = maxsize;
     for(int i = 0; i <max;i++){
         bucket_list[i] = 0; //fill with zeros
       }
       for (unsigned int i = 0; i <length; i++){
           bucket_list[arr[i]]++;
        }
        int position = 0;
        for (unsigned int i = 0 ; i < length ; i++){
           for(int k = 0; k<bucket_list[i];k++){
                arr[position++] = i;
             }
          }
       }
      int main() {
         int max = 50;
         int arr[max];
         for (int i = 0; i < max; i++){
            arr[i] = rand()%50;
            }
            cout<<"Here are the numbers before Bucker Sort"<<endl;
            for (int j = 0; j < max; j++){
                 cout<<arr[j];
              }
            Bucket_sort(arr,max);
            for (int k = 0; k<max; k++){
               cout<<arr[k];
              }
              getch();
              return 0;
           }