| by Arround The Web | No comments

Bucket sort C++

Sorting is a method by which we order the items in a sequence. Bucket sort is one of the sorting algorithms but this algorithm is a bit different from the other algorithms. A bucket, as the name implies, contains something in a separate space like a container. This algorithm places the elements in the bucket according to the condition. The elements are divided into different buckets, and sorting is performed on each bucket. We can decide which algorithm is used to sort the buckets. The other names for bucket sort are bin sort and radix sort. The grouping of elements to be stored in buckets is done uniformly. Bucket sort is the algorithm that is good with small arrays. But when it comes to sorting the larger arrays, this algorithm is not preferred because the complexity increases and the performance decreases. This algorithm is applied mostly on the floating point values where we need to uniformly group the elements of the array.

Advantages:

Using the bucket sort in programming has a few benefits:

  • It is fast because of uniform distribution.
  • The number of comparisons is less than in other sorting techniques.

The bucket sort is preferred when we have to distribute the data and when you are dealing with floating-point values.

Scatter-and-Gather Approach

The bucket sort works on the scatter-and-gather approach. It first scatters the elements in the bucket uniformly. Once they are sorted in the bucket, they are brought together in one place and sorts the array. This technique saves us from the comparison of every array element with each other that increases the complexity and makes the compilation process slow.

The bucket sort sorts the arrays by splitting the array elements into buckets. Each bucket is then stored in a separate location. The storing process can be done using different techniques or algorithms or by recursively applying the bucket sort algorithm.

Example:

Now, we explain the working of bucket sorting with the help of an example. Different methods are employed for the sorting and distribution of the arrays.

#include <algorithm>

#include <iostream>

#include <vector>

using namespace std;
void BucketSort(float array_0[], int n)
{   vector b[n];
    for (int i = 0; i< n; i++) {
        int bi_0 = n * array_0[i];
        b[bi_0].push_back(array_0[i]);
    }
    for (int i = 0; i< n; i++)
        sort(b[i].begin(), b[i].end());

    int index = 0;
    for (int i = 0; i< n; i++)
        for (int j = 0; j < b[i].size(); j++)
            array_0[index++] = b[i][j];
}
int main()
{   float arr[] = { 0.7, 0.5, 0.6, 0.65, 0.4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    BucketSort(arr, n);
cout<< "Sorted array is \n";
    for (int i = 0; i< n; i++)
        cout<<arr[i] << "   ";
    return 0;
}

First of all, include the three important libraries – <algorithm>, <iostream>, and <vector>. The <algorithm> library contains all the methods for sorting and searching. Since we will sort the array of floating point values, it is compulsory to include this library in the code. After this, include the <iostream> library which contains all the built-in methods that we need to input and output the data. The third and last library is <vector>. The vector is like an array but it contains a variable size. Vectors can change the size of an array at runtime; that is their specialty. Since we use the vectors in our code, it is important to import a library that includes the vector in it.

After importing these libraries, use the “namespace std”. Furthermore, define a method named “BucketSort” of return type void. Pass two parameters in it. The first parameter is the float type array “array_0” which is sorted using the bucket sort. The second parameter is an integer type variable “n”. Then, define a vector array of floating point data type. The array is “b” of size “n”. A vector-type array is utilized because the array's size is unknown. Now, add the elements in different buckets using the “for” loop. Declare the “i" iterator and then set the condition to loop until the iterator reaches the number of elements in the array. The “n” attribute shows the array size which is unknown. We observe on how to get the size of an array in the main() method. Increment the “i" iterator. Define a variable in the “for” loop whose size “n” is multiplied by the array values which are stored in the iterator.

In the vector array, employ the push_back() function to push that array element. After pushing the array in buckets, sort these buckets using another “for” loop. Initialize the iterator of the loop and iterate until it reaches the size and keeps incrementing the loop. Sort the array in the body of “for” by calling the sort() method of the algorithm library. Pass two parameters inside this function. The first argument shows the beginning index of the array and the second argument shows the ending index of the array. The begin() function starts the sorting and the end() function stops on the last index. Declare a variable outside the body of “for” to get the index. Use the nested “for” loop to combine the array elements after sorting in separate buckets.

Furthermore, call the main() method. Here, declare a floating point array “arr” and initialize it. Then, define an integer “n” to get the items of the array. The sizeof(arr) function gets the size of an array in bytes. It multiplies the size with 10 and the sizeof(arr[0]) method gets the size of only one element. By dividing both, we can acquire the total components of the array. Now, invoke the BucketSort() method. This function sorts the array by applying the bucket sort algorithm. Represent a “Sorted array is” message on the console using the “cout” command. To display the sorted array, use the “for” loop and set the condition to the number of elements of the array. Use “cout” in the body of “for” to show the updated array.

Conclusion

We discussed the working and implementation of bucket sort in C++. As the name is self-explanatory, it divides the array and stores them in the bucket. After sorting the array, all the buckets are combined in a sequence. This bucket sort is preferred when we deal with floating point values because it divides the buckets based on different conditions and it is used to distribute the data uniformly. The article contains all the information that you should know before implementing the bucket sort. Some different sorting techniques and algorithms are used to sort the array; all the methods have pros and cons. What this method has is the plus point that it is fast and it sorts the array with minimum comparison. But when we have a large array, the bucket sort is not a good option. The article sums up here with a brief overview of the bucket sort.

Share Button

Source: linuxhint.com

Leave a Reply