You are on page 1of 4

School of Computing Science and Engineering

VIT Bhopal University


Ex.No.15 Implementation of Bucket Sort

AIM: TO PERFORM BUCKET SORT

To write and execute Java program to Implementation of Selection Sort


Pseudocode:
Pseudocode
Step 1: function bucketSort(array, n) is
Step 1: buckets ← new array of n empty lists
Step 2: M ← the maximum key value in the array
Step 3: for i = 1 to length(array) do
insert array[i] into buckets[floor(array[i] / M * n)]
Step 4: for i = 1 to n do
sort(buckets[i])
Step 5: return the concatenation of buckets[1], ...., buckets[k]

Explanation:
Step 1:
Since we have the array with integer elements, we'll calculate the range first.
Step 2: Scatter
Now, iterate through the unsorted array and keep inserting the numbers in the
bucket of their corresponding range.
Step 3:
Now sort all the elements in each of the buckets, and the sorted buckets look like
this:
Step 4: Gather
At last, visit each bucket and gather all the numbers together. Merge them all, and
we'll get the sorted array.

Name: Naveen Bara [1] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University
Example:

Program Code:
import java.util.*;
import java.util.Collections;

class bucket {

// JAVA program to perform bucket sorting


// on array of size n
static void bucketSorting(float array[], int n)
{
if (n <= 0)
return;

// 1) Create n empty buckets


Vector<Float>[] buckets = new Vector[n];

for (int i = 0; i < n; i++) {


buckets[i] = new Vector<Float>();
}

// 2) Put array elements in different buckets


for (int i = 0; i < n; i++) {
float idx = array[i] * n;
buckets[(int)idx].add(array[i]);
}

// 3) Sort individual buckets

Name: Naveen Bara [2] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University
for (int i = 0; i < n; i++) {
Collections.sort(buckets[i]);
}

// 4) Gather all buckets together into array[]


int arrayIndex = 0;
for (int i = 0; i < n; i++) {
int bucketSize = buckets[i].size();
for (int k = 0; k < bucketSize; k++) {
array[arrayIndex ++] = buckets[i].get(k);
}
}
}
static void printArr(float array[], int n)
{
for (int i = 0; i < n; ++i)
System.out.print(array[i] + " ");
}

// Driver code
public static void main(String args[])
{
float array[] = { (float)0.42, (float)0.32,
(float)0.35, (float)0.52,
(float)0.39, (float)0.47,
(float)0.50};

int n = array.length;
System.out.print("Before sorting array elements are - \n");
printArr(array, n);
bucketSorting(array, n);

System.out.println("");
System.out.println("Sorted Array is: ");
for (float element : array) {
System.out.print(element + " ");
}
}
}

Output Screenshots:
correct input

Name: Naveen Bara [3] Reg. Number: 22MCA10116


School of Computing Science and Engineering
VIT Bhopal University

RESULT: Implementation of Bucket Sort Thus, the programs for the given problem statements has been
executed and the results are verified successfully.

Name: Naveen Bara [4] Reg. Number: 22MCA10116

You might also like