A bucket sort begins with an one dimensional array of


Bucket Sort

A bucket sort begins with an one dimensional array of positive integers to be sorted, and a two dimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to n - 1 where n is the number of values in the array to be sorted. Each row of the two dimensional array is referred to as a bucket. Write a function bucketSort that takes an integer array and the array size as arguments.

The algorithm is as follows:

1) Loop through the one- dimensional array and place each of its values in a row of the bucket array based on its ones digit. For example, 97 is placed in row 7, 3 is placed in row 3 and 100 is placed in row 0.

2) Loop through the bucket array and copy the values back to the original array. The new order of the above values in the one dimensional array is 100, 3 and 97.

3) Repeat this process for each subsequent digit position (tens, hundreds, thousands, etc.) and stop when the leftmost digit of the largest number has be processed.On the second pass of the array, 100 is placed in row 0, 3 is placed in row 0 (it had only one digit) and 97 is placed in row 9. The order of the values in the one- dimensional array is 100, 3 and 97. On the third pass, 100 is placed in row 1, 3 is placed in row zero and 97 is placed in row zero (after 3). The bucket sort is guaranteed to have all the values properly sorted after processing the leftmost digit of the largest number. The bucket sort knows it is done when all the values are copied into row zero of the two dimensional array.

The two dimensional array of buckets is ten times the size of the integer array being sorted. This sorting technique provides better performance than a bubble sort, but requires much larger storage capacity. Bubble sort requires only one additional memory location for the type of data being sorted. Bucket sort is an example of a space-time trade-off. It uses more memory, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two dimensional bucket array and repeatedly move the data between the two bucket arrays until all the data is copied into row zero of one of the arrays. Row zero then contains the sorted array.

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: A bucket sort begins with an one dimensional array of
Reference No:- TGS01256878

Now Priced at $25 (50% Discount)

Recommended (96%)

Rated (4.8/5)