Guest User

Untitled

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