The Full Counting Sort

Sort by

recency

|

718 Discussions

|

  • + 0 comments

    python solution

    def countSort(arr):
        n = len(arr)
        sort = [ [ ] for _ in range(n)]
        for i, pair in enumerate(arr):
            if i < n// 2:
                pair[1] = "-"
            sort[int(pair[0])].append(pair[1])
        
        print(" ".join([char for line in sort for char in line]))
    
  • + 0 comments

    public static void countSort(List> arr) { // Write your code here int cnt = arr.size(); int hc = cnt / 2;

        List<List<String>> sc = new ArrayList<>(cnt);
    
        for (int i = 0; i < cnt; i++) {
            sc.add(new ArrayList<>());
        }
    
        int i = 0;
        for (int j = 0; j < cnt; j++){
            int key = Integer.parseInt(arr.get(j).get(0));
            String vlu = arr.get(j).get(1);
    
            if(i<hc){
                vlu = "-";
            }
            sc.get(key).add(vlu);
            i++;
        };
    
        sc.forEach((t) -> {
            if(!t.isEmpty()){
              System.out.print(String.join(" ", t)+ " ");
            }
        });
    }
    
  • + 0 comments

    I've been struggling a lot to get the Test number 5 to run within time limits. Here is my approach in C# - hope it helps someone.

        public static void countSort(List<List<string>> arr){
            var buckets = new List<string>[100];
            for(int i=0; i<100; i++){
                buckets[i] = new List<string>();
            }
            
            var count = arr.Count();
            var half = count / 2;
            
            for(int i=0; i<count; i++){
                var index = int.Parse(arr[i][0]);
                var value = arr[i][1];
                if(i<half){
                    value = "-";    
                }
                buckets[index].Add(value);
            }
                  
            for(int i=0; i<100; i++){
                var size = buckets[i].Count();
                if (size > 0){
                    Console.Write(string.Join(" ", buckets[i]) + " ");
                }
            }
        }
    
  • + 0 comments

    My answer in Typescript, noted

    function countSort(arr: string[][]): void {
        // create converted arr, fill with []
        let arr_converted = Array.from({ length: arr.length }, () => []);
    
        // forech element in [arr], add to [arr_converted]
        for (let i = 0; i < arr.length; i++) {
            let n = Number(arr[i][0])
            let c = i + 1 <= arr.length / 2 ? '-' : arr[i][1]
    
            arr_converted[n].push(c)
        }
    
        // print
        console.log(arr_converted.flat().join(' ').trim())
    }
    
  • + 1 comment

    Python

    def countSort(arr):
        for i in range(len(arr)//2):
            arr[i][1] = '-'
        arr.sort(key=lambda x: int(x[0]))
        [print(c[1], end=' ') for c in arr]