Merge pull request #1450 from shellhub/dev-1

Updated BucketSort
This commit is contained in:
Du Yuanchao 2020-09-08 15:08:55 +08:00 committed by GitHub
commit ddd98a9a4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,72 +1,115 @@
package Sorts;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class BucketSort
{
static int[] sort(int[] sequence, int maxValue)
{
// Bucket Sort
int[] Bucket = new int[maxValue + 1];
int[] sorted_sequence = new int[sequence.length];
for (int i = 0; i < sequence.length; i++)
Bucket[sequence[i]]++;
int outPos = 0;
for (int i = 0; i < Bucket.length; i++)
for (int j = 0; j < Bucket[i]; j++)
sorted_sequence[outPos++] = i;
return sorted_sequence;
}
static void printSequence(int[] sorted_sequence)
{
for (int i = 0; i < sorted_sequence.length; i++)
System.out.print(sorted_sequence[i] + " ");
}
static int maxValue(int[] sequence)
{
int maxValue = 0;
for (int i = 0; i < sequence.length; i++)
if (sequence[i] > maxValue)
maxValue = sequence[i];
return maxValue;
}
public static void main(String args[])
{
System.out.println("Sorting of randomly generated numbers using BUCKET SORT");
/**
* Wikipedia: https://en.wikipedia.org/wiki/Bucket_sort
*/
public class BucketSort {
public static void main(String[] args) {
int[] arr = new int[10];
/* generate 10 random numbers from -50 to 49 */
Random random = new Random();
int N = 20;
int[] sequence = new int[N];
for (int i = 0; i < N; i++)
sequence[i] = Math.abs(random.nextInt(100));
int maxValue = maxValue(sequence);
System.out.println("\nOriginal Sequence: ");
printSequence(sequence);
System.out.println("\nSorted Sequence: ");
printSequence(sort(sequence, maxValue));
for (int i = 0; i < arr.length; ++i) {
arr[i] = random.nextInt(100) - 50;
}
bucketSort(arr);
/* check array is sorted or not */
for (int i = 0, limit = arr.length - 1; i < limit; ++i) {
assert arr[i] <= arr[i + 1];
}
}
/**
* BucketSort algorithms implements
*
* @param arr the array contains elements
*/
private static void bucketSort(int[] arr) {
/* get max value of arr */
int max = max(arr);
/* get min value of arr */
int min = min(arr);
/* number of buckets */
int numberOfBuckets = max - min + 1;
List<List<Integer>> buckets = new ArrayList<>(numberOfBuckets);
/* init buckets */
for (int i = 0; i < numberOfBuckets; ++i) {
buckets.add(new ArrayList<>());
}
/* store elements to buckets */
for (int value : arr) {
int hash = hash(value, min, numberOfBuckets);
buckets.get(hash).add(value);
}
/* sort individual bucket */
for (List<Integer> bucket : buckets) {
Collections.sort(bucket);
}
/* concatenate buckets to origin array */
int index = 0;
for (List<Integer> bucket : buckets) {
for (int value : bucket) {
arr[index++] = value;
}
}
}
/**
* Get index of bucket which of our elements gets placed into it.
*
* @param elem the element of array to be sorted
* @param min min value of array
* @param numberOfBucket the number of bucket
* @return index of bucket
*/
private static int hash(int elem, int min, int numberOfBucket) {
return (elem - min) / numberOfBucket;
}
/**
* Calculate max value of array
*
* @param arr the array contains elements
* @return max value of given array
*/
public static int max(int[] arr) {
int max = arr[0];
for (int value : arr) {
if (value > max) {
max = value;
}
}
return max;
}
/**
* Calculate min value of array
*
* @param arr the array contains elements
* @return min value of given array
*/
public static int min(int[] arr) {
int min = arr[0];
for (int value : arr) {
if (value < min) {
min = value;
}
}
return min;
}
}
/*
#Output:
$ javac Bucket_Sort.java
$ java Bucket_Sort
Sorting of randomly generated numbers using BUCKET SORT
Original Sequence:
95 9 95 87 8 81 18 54 57 53 92 15 38 24 8 56 29 69 64 66
Sorted Sequence:
8 8 9 15 18 24 29 38 53 54 56 57 64 66 69 81 87 92 95 95
*/