The Full Counting Sort

  • + 0 comments

    Using comparator and a custom class :

    class Arstr{
        private int pos ;
        private String str;
        public Arstr(Integer i,String s1) {
            this.pos = i;
            this.str=s1;
        }
        public int getpos() {
            return this.pos;
        }
        public String getstr() {
            return this.str;
        }
    }
    
    class Result {
    
        /*
         * Complete the 'countSort' function below.
         *
         * The function accepts 2D_STRING_ARRAY arr as parameter.
         */
    
        public static void countSort(List<List<String>> arr) {
        // Write your code here
        List<Arstr> res = new ArrayList<>();
            
             Comparator<Arstr> comp = new Comparator<Arstr>(){
                 
                    @Override
                    public int compare(Arstr o1, Arstr o2) {
                        
                        return (o1.getpos() - o2.getpos());
                    }
             };
            
            for(int i=0;i<arr.size();i++) {
                if(i<arr.size()/2) {
                    res.add(new Arstr(Integer.valueOf(arr.get(i).get(0)),"-"));
                }
                else {
                res.add(new Arstr(Integer.valueOf(arr.get(i).get(0)),arr.get(i).get(1)));
                }
            }
            Collections.sort(res,comp);
            for(Arstr a:res) {
                System.out.print(a.getstr()+" ");
            }
        }
    
    }