-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Bucket Sort Implementation in Java (#47)
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Java program to sort an array | ||
// using bucket sort | ||
import java.util.*; | ||
import java.util.Collections; | ||
|
||
class BucketSort { | ||
|
||
// Function to sort arr[] of size n | ||
// using bucket sort | ||
static void bucketSort(float arr[], int n) | ||
{ | ||
if (n <= 0) | ||
return; | ||
|
||
// 1) Create n empty buckets | ||
@SuppressWarnings("unchecked") | ||
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 = arr[i] * n; | ||
buckets[(int)idx].add(arr[i]); | ||
} | ||
|
||
// 3) Sort individual buckets | ||
for (int i = 0; i < n; i++) { | ||
Collections.sort(buckets[i]); | ||
} | ||
|
||
// 4) Concatenate all buckets into arr[] | ||
int index = 0; | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < buckets[i].size(); j++) { | ||
arr[index++] = buckets[i].get(j); | ||
} | ||
} | ||
} | ||
|
||
// Driver code | ||
public static void main(String args[]) | ||
{ | ||
float arr[] = { (float)0.897, (float)0.565, | ||
(float)0.656, (float)0.1234, | ||
(float)0.665, (float)0.3434 }; | ||
|
||
int n = arr.length; | ||
bucketSort(arr, n); | ||
|
||
System.out.println("Sorted array is "); | ||
for (float el : arr) { | ||
System.out.print(el + " "); | ||
} | ||
} | ||
} | ||
|