|
| 1 | +/** |
| 2 | + * |
| 3 | + * Bucket Sort Algorithm |
| 4 | + * |
| 5 | + * @author yashashvi (github.com/YJDave) |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Definition for Bucket Sort: |
| 12 | + * Wikipedia: https://en.wikipedia.org/wiki/Bucket_sort |
| 13 | + * Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. |
| 14 | + * Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. |
| 15 | + * |
| 16 | + * Time complexity: |
| 17 | + * n: length of input sequence |
| 18 | + * k: number of buckets |
| 19 | + * Average case: o(n+k) |
| 20 | + * Worst case: o(n^2) |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * Implementation of Bucket Sort: |
| 27 | + * 1. Create and Initialize all Buckets |
| 28 | + * 2(i). Take input from user (any unsorted numbers) |
| 29 | + * 2(ii). Insert this numbers into specific Bucket according to HashFunction |
| 30 | + * (we use here HashFunction to find that in which Bucket number should be added) |
| 31 | + * (you can use your custom HashFunction depending on input data's property) |
| 32 | + * |
| 33 | + * 3. Sort all Bucket (you can any sorting algorithm, here I have used Insertion Sort) |
| 34 | + * 4. Place sorted no into SortedArray from Bucket |
| 35 | + * 5. Print SortedArray |
| 36 | + * |
| 37 | + * Data-structure used: |
| 38 | + * Array: for storing sorted and unsorted numbers |
| 39 | + * Double Dimentional Array: for storing Buckets |
| 40 | + * (I have used double dimenstional array for simple implementation but, |
| 41 | + * If you want you can use linked list for storing buckets, for efficient usage of memory) |
| 42 | + * |
| 43 | + */ |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +import java.util.Scanner; |
| 48 | + |
| 49 | +public class BucketSort { |
| 50 | + |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + |
| 54 | + int MaxBuckets = 10; |
| 55 | + |
| 56 | + |
| 57 | + System.out.println("Sort numbers using Bucket Sort Alogrithm"); |
| 58 | + |
| 59 | + Scanner input = new Scanner(System.in); |
| 60 | + System.out.print("Enter total no of input: "); |
| 61 | + int totalNo = input.nextInt(); |
| 62 | + |
| 63 | + // 1. Create and Initialize all Buckets |
| 64 | + |
| 65 | + int[][] Buckets = new int[MaxBuckets][totalNo+1]; |
| 66 | + System.out.println(totalNo+1); |
| 67 | + // we use Buckets[0][*] for storing array top |
| 68 | + // intialize it to 0 |
| 69 | + |
| 70 | + for(int i=0;i<MaxBuckets;i++) |
| 71 | + Buckets[i][0] = 1; |
| 72 | + |
| 73 | + // 2(i). Take input from user (any unsorted numbers) |
| 74 | + |
| 75 | + System.out.println("Enter all no:"); |
| 76 | + |
| 77 | + int[] inputArray = new int[totalNo]; |
| 78 | + int inputIndex = 0; |
| 79 | + |
| 80 | + for(int i=0;i<totalNo;i++) { |
| 81 | + |
| 82 | + int no = input.nextInt(); |
| 83 | + inputArray[inputIndex++] = no; |
| 84 | + } |
| 85 | + input.close(); |
| 86 | + |
| 87 | + // 2(ii). Insert this numbers into specific Bucket |
| 88 | + |
| 89 | + for(int i=0;i<totalNo;i++) { |
| 90 | + // System.out.println("\n"+"Deciding for no: "+inputArray[i]); |
| 91 | + int hashKey = HashFunction(inputArray[i]); |
| 92 | + int top = Buckets[hashKey][0]; |
| 93 | + |
| 94 | + // System.out.println("Hash key is: "+hashKey+" and top is: "+top+" so we will store no at top and increase top"); |
| 95 | + Buckets[hashKey][top] = inputArray[i]; |
| 96 | + Buckets[hashKey][0] = top+1; |
| 97 | + |
| 98 | + // System.out.println("So our Bucket is: +"+"\n"+ "Bucket["+hashKey+"]["+0+"] = "+Buckets[hashKey][0]+" and "+"Bucket["+hashKey+"]["+top+"] = "+Buckets[hashKey][top]); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + // 3. Sort all Bucket (you can any sorting algorithm, here I have used Insertion Sort) |
| 104 | + |
| 105 | + for(int i=0;i<MaxBuckets;i++) { |
| 106 | + int top = Buckets[i][0]; |
| 107 | + |
| 108 | + // System.out.println("Sorting Bucket no:"+i); |
| 109 | + for(int k=1;k<top-1;k++) { |
| 110 | + int j = k +1; |
| 111 | + int temp = Buckets[i][j]; |
| 112 | + while( k>0 && Buckets[i][k]>temp) { |
| 113 | + Buckets[i][k+1] = Buckets[i][k]; |
| 114 | + k--; |
| 115 | + } |
| 116 | + Buckets[i][k+1] = temp; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + for(int i=0;i<MaxBuckets;i++) { |
| 121 | + int top = Buckets[i][0]; |
| 122 | + for(int j=0;j<top;j++) { |
| 123 | + System.out.println("Bucket["+i+"]["+j+"] = "+Buckets[i][j]); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // 4. Place sorted no into SortedArray from Bucket |
| 128 | + |
| 129 | + int[] SortedArray = new int[totalNo]; |
| 130 | + int sortedIndex=0; |
| 131 | + |
| 132 | + for(int i=0;i<MaxBuckets;i++) { |
| 133 | + int top = Buckets[i][0]; |
| 134 | + for(int j=1;j<top;j++) { |
| 135 | + SortedArray[sortedIndex++] = Buckets[i][j]; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + // 5. Print SortedArray |
| 141 | + |
| 142 | + System.out.println("Sorted Array is: "); |
| 143 | + for(int i=0;i<totalNo;i++) |
| 144 | + System.out.print(SortedArray[i]+", "); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + public static int HashFunction(int no) { |
| 149 | + |
| 150 | + int Key = no/10; |
| 151 | + if (Key>9) |
| 152 | + Key=9; |
| 153 | + // System.out.println(Key); |
| 154 | + return Key; |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | +} |
0 commit comments