Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Solution {
  4. /**
  5. * Sorts a list of Dutch mobile phone numbers using LSD radix sort.
  6. *
  7. * @param phoneNumbers
  8. * The list of phone numbers to sort.
  9. * @return The sorted list of phone numbers.
  10. * @throws NullPointerException
  11. * If `phoneNumbers` equals `null`.
  12. */
  13. static List<String> radixSortLSD(List<String> phoneNumbers) {
  14. if (phoneNumbers == null){
  15. throw new NullPointerException();
  16. }
  17.  
  18. ArrayList<String> list = new ArrayList<>();
  19. list.addAll(phoneNumbers);
  20.  
  21. Queue<String>[] buckets = new LinkedList[10];
  22. for (int i = 0; i<buckets.length; i++){
  23. buckets[i] = new LinkedList<String>();
  24. }
  25.  
  26.  
  27. for (int j = 9; j>=2; j--){
  28. for (int i = 0; i<list.size(); i++){
  29. buckets[Character.getNumericValue(list.get(i).charAt(j))].add(list.get(i));
  30. }
  31. list.clear();
  32. for (int i=0; i<buckets.length; i++){
  33. list.addAll(buckets[i]);
  34. buckets[i].clear();
  35. }
  36. }
  37. return list;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement