Guest User

Untitled

a guest
Oct 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 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. ArrayList<HashNode<String, V>> aL= MSDHelper(myList, 0);
  15. HashNode<String, V>[] hashNodeArray = new HashNode[aL.size()];
  16. aL.toArray(hashNodeArray);
  17. return null;
  18. }
  19.  
  20. private ArrayList<HashNode<String, V>> MSDHelper(
  21. ArrayList<HashNode<String, V>> arr, int curPlace) {
  22. int index = 0;
  23. @SuppressWarnings("unchecked")
  24. // As the name suggest this is a bucket
  25. ArrayList<HashNode<String, V>>[] buckets = (ArrayList<HashNode<String, V>>[]) new ArrayList[129];
  26. ArrayList<HashNode<String, V>> myList = new ArrayList<HashNode<String, V>>();
  27.  
  28. for (int i = 0; i < arr.size(); i++) {
  29. // First HashNode in the index that we're working in
  30. HashNode<String, V> curr = arr.get(i);
  31.  
  32. if (curr.getKey().length() <= curPlace) {
  33. if (buckets[0] == null)// IDK if this is actually required, but
  34. // I just did it. Same for the other if
  35. // statement
  36. buckets[0] = new ArrayList<HashNode<String, V>>();
  37. buckets[0].add(curr);
  38. } else { // this follows from the PDF
  39. index = (int) curr.getKey().charAt(curPlace);
  40.  
  41. if (buckets[index + 1] == null)
  42. buckets[index + 1] = new ArrayList<HashNode<String, V>>();
  43.  
  44. buckets[index + 1].add(curr);
  45. }
  46. }
  47. for (int j = 1; j < 129; j++) {
  48. if (buckets[j] != null && buckets[j].size() > 1)
  49. MSDHelper(buckets[j], curPlace + 1);
  50. }
  51.  
  52. for (int i = 0; i < buckets.length; i++) {
  53. if (buckets[i] != null) {
  54. for (int j = 0; j < buckets[i].size(); j++)
  55. myList.add(buckets[i].get(j));
  56. }
  57. }
  58.  
  59. return myList;
  60.  
  61. }
Add Comment
Please, Sign In to add comment