Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Solution {
- /**
- * Sorts a list of Dutch mobile phone numbers using LSD radix sort.
- *
- * @param phoneNumbers
- * The list of phone numbers to sort.
- * @return The sorted list of phone numbers.
- * @throws NullPointerException
- * If `phoneNumbers` equals `null`.
- */
- static List<String> radixSortLSD(List<String> phoneNumbers) {
- if (phoneNumbers == null){
- throw new NullPointerException();
- }
- ArrayList<String> list = new ArrayList<>();
- list.addAll(phoneNumbers);
- Queue<String>[] buckets = new LinkedList[10];
- for (int i = 0; i<buckets.length; i++){
- buckets[i] = new LinkedList<String>();
- }
- for (int j = 9; j>=2; j--){
- for (int i = 0; i<list.size(); i++){
- buckets[Character.getNumericValue(list.get(i).charAt(j))].add(list.get(i));
- }
- list.clear();
- for (int i=0; i<buckets.length; i++){
- list.addAll(buckets[i]);
- buckets[i].clear();
- }
- }
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement