Guest User

Untitled

a guest
Oct 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public HashNode<String,V>[] sort(HashNode<String, V>[]arr){
  2. ArrayList<HashNode<String,V>> hashList = new ArrayList<HashNode<String,V>>(Arrays.asList(arr));
  3. for(int i = 0; i<arr.length; i++){
  4. arr[i] = hashList.get(i);
  5. }
  6. return (HashNode<String,V>[])MSDHelper(hashList, 0).toArray();
  7.  
  8. }
  9. public ArrayList<HashNode<String, V>> MSDHelper(ArrayList<HashNode<String,V>> arr, int curPlace){
  10. ArrayList<HashNode<String, V>>[] array = (ArrayList<HashNode<String, V>>[])new ArrayList[129];
  11. //Initialize each value in the array.
  12. for(int i = 0; i<array.length; i++){
  13. array[i] = new ArrayList<HashNode<String, V>>();
  14. }
  15. //Loop through the array and put the values that are less than the curPlace in the 0th index and values bigger than curPlace into the next available index.
  16. for(int k = 0; k<arr.size(); k++){
  17. if(arr.get(k).getKey().length() <= curPlace){
  18. array[0].add(arr.get(k));
  19. }
  20. else {
  21. array[arr.get(k).getKey().charAt(curPlace+1)].add(arr.get(k));
  22. }
  23. }
  24. //Loop through the array to recursively sort the elements in the bucket.
  25. for (int j = 0; j < array.length; j++) {
  26. if (array[j].size() > 1){
  27. array[j] = MSDHelper(array[j], curPlace + 1);
  28. }
  29. }
  30. //Merge it all back together.
  31. for (int j = 1; j < array.length; j++) {
  32. array[0].addAll(array[j]);
  33. }
  34. return array[0];
  35. }
Add Comment
Please, Sign In to add comment