We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticvoidcountSort(List<List<String>>arr){intmiddle=arr.size()/2;inttemp[]=newint[100];Map<Integer,List<String>>map=newHashMap<>();// Traverse the list and populate hashmap// with keys being the "weight" of the pair.// Also replace the str to "-" if it is between [0..n/2].for(inti=0;i<arr.size();i++){List<String>a=arr.get(i);intweight=Integer.parseInt(a.get(0));Stringstr=a.get(1);if(i<middle){str="-";}temp[weight]++;if(map.containsKey(weight)){map.get(weight).add(str);}else{map.put(weight,newArrayList<String>());map.get(weight).add(str);}}// The base of count sort adapted to // this challenge.for(inti=0;i<temp.length;i++){intrepeat=temp[i];for(intj=0;j<repeat;j++){System.out.print(map.get(i).get(j)+" ");}}}
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →