Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public HashNode<String, V>[] sort(HashNode<String, V>[] arr) {
- // This ArrayList is just gonna hold all the nodes which will be
- // inserted one by one
- ArrayList<HashNode<String, V>> myList = new ArrayList<HashNode<String, V>>();
- HashNode<String, V> probe;
- for (int i = 0; i < arr.length; i++) {
- myList.add(arr[i]);
- probe = arr[i];
- while (probe.getNext() != null) {
- myList.add(probe.getNext());
- probe = probe.getNext();
- }
- }
- // I don't get what to return. Also I don't get where to call the helper
- // method
- return (HashNode<String, V>[]) MSDHelper(myList, 0).toArray();
- }
- private ArrayList<HashNode<String, V>> MSDHelper(
- ArrayList<HashNode<String, V>> arr, int curPlace) {
- int index = 0;
- @SuppressWarnings("unchecked")
- // As the name suggest this is a bucket
- ArrayList<HashNode<String, V>>[] buckets = (ArrayList<HashNode<String, V>>[]) new Object[129];
- ArrayList<HashNode<String, V>> myList = new ArrayList<HashNode<String, V>>();
- for (int i = 0; i < arr.size(); i++) {
- // First HashNode in the index that we're working in
- HashNode<String, V> curr = arr.get(i);
- if (curr.getKey().length() <= curPlace) {
- if (buckets[0] == null)// IDK if this is actually required, but
- // I just did it. Same for the other if
- // statement
- buckets[0] = new ArrayList<HashNode<String, V>>();
- buckets[0].add(curr);
- } else { // this follows from the PDF
- index = (int) curr.getKey().charAt(curPlace);
- if (buckets[index + 1] == null)
- buckets[index + 1] = new ArrayList<HashNode<String, V>>();
- buckets[index + 1].add(curr);
- }
- }
- for (int j = 1; j < 129; j++) {
- if (buckets[j] != null && buckets[j].size() > 1)
- MSDHelper(buckets[j], curPlace + 1);
- }
- for (int i = 0; i < buckets.length; i++) {
- if (buckets[i] != null) {
- for (int j = 0; j < buckets[i].size(); j++)
- myList.add(buckets[i].get(j));
- }
- }
- return myList;
- }
Add Comment
Please, Sign In to add comment