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