Reference no: EM132196881
Write a class called Bag.java which will sort in ascending order an array arr[] of double from 0 to 1, using the following algorithm.
Indicate in comment, the Time Complexity of your algorithm.
1. Create n bags , n being the number of element in the array.
2. For every element of the array , put this element in a bag so that arr[ i ] is inserted into bag number n*arr [i] + 1 .
3. Sort each bags individually using the Selection sort.
4. Put all those values back in the array arr[] so that the array is now sorted.
Example :
Assume the array arr : 0.15 0.25 0.5 0.18 0.014 0.2 We have 6 elements, therefore 6 bags, which are filled as followed The elements in Bag 1 are sorted , as well as the element in Bag 2. The new sorted array is : 0.014 0.15 0.18 0.2 0.25 0.5
Note : This algorithm is a variation of the Bucket sorting algorithm.