Guest User

Untitled

a guest
Oct 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public HashNode<String, V>[] sort(HashNode<String, V>[] arr) {
  2. // This ArrayList is just gonna hold all the nodes which will be
  3. // inserted one by one
  4. ArrayList<HashNode<String, V>> myList = new ArrayList<HashNode<String, V>>();
  5. HashNode<String, V> probe;
  6. for (int i = 0; i < arr.length; i++) {
  7. myList.add(arr[i]);
  8. probe = arr[i];
  9. while (probe.getNext() != null) {
  10. myList.add(probe.getNext());
  11. probe = probe.getNext();
  12. }
  13. }
  14. // I don't get what to return. Also I don't get where to call the helper
  15. // method
  16. return (HashNode<String, V>[]) MSDHelper(myList, 0).toArray();
  17. }
  18.  
  19. private ArrayList<HashNode<String, V>> MSDHelper(
  20. ArrayList<HashNode<String, V>> arr, int curPlace) {
  21. int index = 0;
  22. @SuppressWarnings("unchecked")
  23. // As the name suggest this is a bucket
  24. ArrayList<HashNode<String, V>>[] buckets = (ArrayList<HashNode<String, V>>[]) new Object[129];
  25. ArrayList<HashNode<String, V>> myList = new ArrayList<HashNode<String, V>>();
  26.  
  27. for (int i = 0; i < arr.size(); i++) {
  28. // First HashNode in the index that we're working in
  29. HashNode<String, V> curr = arr.get(i);
  30.  
  31. if (curr.getKey().length() <= curPlace) {
  32. if (buckets[0] == null)// IDK if this is actually required, but
  33. // I just did it. Same for the other if
  34. // statement
  35. buckets[0] = new ArrayList<HashNode<String, V>>();
  36. buckets[0].add(curr);
  37. } else { // this follows from the PDF
  38. index = (int) curr.getKey().charAt(curPlace);
  39.  
  40. if (buckets[index + 1] == null)
  41. buckets[index + 1] = new ArrayList<HashNode<String, V>>();
  42.  
  43. buckets[index + 1].add(curr);
  44. }
  45. }
  46. for (int j = 1; j < 129; j++) {
  47. if (buckets[j] != null && buckets[j].size() > 1)
  48. MSDHelper(buckets[j], curPlace + 1);
  49. }
  50.  
  51. for (int i = 0; i < buckets.length; i++) {
  52. if (buckets[i] != null) {
  53. for (int j = 0; j < buckets[i].size(); j++)
  54. myList.add(buckets[i].get(j));
  55. }
  56. }
  57.  
  58. return myList;
  59.  
  60. }
Add Comment
Please, Sign In to add comment