The Full Counting Sort

Sort by

recency

|

78 Discussions

|

  • + 0 comments

    Python 3 solution:

    def countSort(arr: list[list[str]]) -> None:
        for i in range(len(arr) // 2):
            arr[i][1] = "-"
        arr.sort(key=lambda x: int(x[0]))
        print(*(x[1] for x in arr))
    
  • + 0 comments
    1. create empty array of 100 arrays
    2. add elements acc to index mentioned
    3. remove empty elements
    4. convert to string

    def countSort(arr):

    arr_sort = [[] for _ in range(100)]
    arr_len = len(arr)
    for j in range(arr_len):
        f = int(arr[j][0])
        s = arr[j][1]
        if j < arr_len // 2:
            arr_sort[f].append('-')
        else:
            arr_sort[f].append(s)
    
    res = [y for x in arr_sort for y in x]
    print(" ".join(res))
    
  • + 0 comments

    This task has the most lack of understandable description in easy way, through 75 of previous tasks. c++

    void countSort(vector<vector<string>> arr) {
        const int n = arr.size();
        multimap<int, string> m;
        
        for (int i = 0; i < n; ++i)
        {
            m.emplace(stoi(arr[i][0]), i < n / 2 ? "-" : arr[i][1]);
        }
        
        for (const auto& p : m)
        {
            cout << p.second << ' ';
        }
    }
    
  • + 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()+" ");
            }
        }
    
    }
    
  • + 0 comments

    Typescript

    function countSort(arr: string[][]): void {
      const arrHalfLen = Math.ceil(arr.length / 2);
      let result = [];
      const _arr = [];
    
      for (let i = 0; i < arr.length; i++) {
        if (i < arrHalfLen) {
          _arr.push([arr[i][0], '-']);
          continue;
        }
        _arr.push([arr[i][0], arr[i][1]]);
      }
    
      _arr.sort((a: any, b: any) => a[0] - b[0]);
    
      for (let i = 0; i < _arr.length; i++) {
        result.push(_arr[i][1]);
      }
    
      console.log(result.join(' '));
    }