So, this is what I've got down so far with my BucketSort.java I created two buckets, master bucket and sub buckets[10].
- Ask user to enter integers from 0 - 9999
- Take user input and add to master bucket
- Now use BucketSort method to take from master bucket and sort into sub buckets
- Then use other method to take elements in sub bucket starting from bucket[9] and return to mast bucket
I got the sorting method down, and understand what is happening. But now what puzzles me is what happens if the user inputs different sizes? For example they input [1234, 123, 12, 1]. My sorting method would only sort either size 4, size 3, size 2, or size 1 but not all simultaneously. Is there a way to create a method that will help you sort all sizes? Mine is only formatted for the user to only enter sizes 4.
public static void MtoS(int p)//user inputs 3, 2, 1, 0 for p { while(!(mBucket.isEmpty())) { String a = (String)mBucket.remove(0);//remove element(0) char c = a.charAt(p);//element(0) at position p int k = (int)c - (int)'0';//use to convert char to int for(int n = 0; n < 10; n++) { if(k == n)//if element(0) at charAt(3) equals to n { sBucket[n].add(a);//keep looping till p == n, then adds to corresponding sBucket } } }