Bucket Sort in C and C++

Here you will get program for bucket sort in C and C++.

In bucket sort algorithm the array elements are distributed into a number of buckets. Then each bucket sorted individually either using any other sorting algorithm or by recursively applying bucket sort.

Take example shown in below image.

Bucket Sort

Elements are distributed among buckets

Bucket Sort

Then, elements are sorted within each bucket

Below is the program to implement this algorithm. In this program it is assumed that the array elements are between 0 to 10.

Program for Bucket Sort in C


Before Sorting
5 -34 10 1 -42 123 2 395 5 4 1234 7
After Sorting
-42 -34 1 2 4 5 5 7 10 123 395 1234

Program for Bucket Sort in C++


Before Sorting
0.235 0.101 0.476 0.7645 0.15 0.142
After Sorting
0.101 0.142 0.15 0.235 0.476 0.7645

8 thoughts on “Bucket Sort in C and C++”

  1. ohh…hello it’s not bucket sort ,your coding is almost like counting sort……….u r making a fool of us …..first learn what bucket sort is okk

  2. This is not Bucket Sort but Counting Sort – the difference is that in Counting Sort you count how much times the number appears in array (like in your code) which is good for integers but bad for floats. Bucket Sort works with floats – it creates “buckets” – array of lists, divide elements by liknking them to appropriate lists (that every element in list a[i-1] are lower than element in list a[i], every elements in list a[i] are lower than every element in list a[i+1] etc.) next you sort elements in lists using whatever you want (there are few elements than this can be Insertion Sort – will be fast) and then you rewrite sorted elements form lists back to array.

  3. This is counting sort, brthr!!
    If you are saying it bucket_sort, the how can you say a bucket can have only 1 element.
    Plzz do post only you are fully sure!!!

  4. Hey? Can I ask something? What is the code when the users enter 10 numbers by using bucket sort in html? Thankyouuu in advance.

Leave a Comment

Your email address will not be published. Required fields are marked *