The Full Counting Sort

Sort by

recency

|

82 Discussions

|

  • + 0 comments
    public static void countSort(List<List<String>> arr) {
    // Write your code here
    List<List<String>> buckets = new ArrayList<>();
    for(int i =0; i < 100; i++){
        buckets.add(new ArrayList<>());
    }
    int n= arr.size();
    for(int i =0; i < n ; i++){
        Integer key = Integer.parseInt(arr.get(i).get(0));
        String str = arr.get(i).get(1);
        if(i < n /2){
            str = "-";
        }
        buckets.get(key).add(str);
    }
    StringBuilder sb = new StringBuilder();
    for(List<String> bucket : buckets){
        for(String s : bucket){
            sb.append(s).append(" ");
        }
    }
    System.out.println(sb.toString());
    }
    
  • + 0 comments

    Python solution:

    def countSort(arr):
        mid_point = len(arr)//2
        mid_point_arr = [[elem[0], "-"] for elem in arr[:mid_point]]
        dashed_arr = mid_point_arr + arr[mid_point:]
        dashed_arr.sort(key=lambda elem: int(elem[0]))
        [print(elem[1], end=" ") for elem in dashed_arr]
    
  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn full_countint_sort(arr: &[Vec<String>]) {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(1)
        let mut sorted_array: Vec<String> = vec!["".to_string(); 101];
        for index in 0..arr.len() {
            let sorted_index = arr[index][0]
                .parse::<usize>()
                .expect("To be able to parse correctly");
            if index < arr.len() / 2 {
                sorted_array[sorted_index].push_str(" -");
            } else {
                sorted_array[sorted_index].push(' ');
                sorted_array[sorted_index].push_str(&arr[index][1]);
            }
        }
        let mut result_array = String::new();
        for string in sorted_array {
            if !string.is_empty() {
                if result_array.is_empty() {
                    result_array = string[1..].to_string();
                } else {
                    result_array.push_str(&string);
                }
            }
        }
        println!("{}", result_array);
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def full_counting_sort(arr):
        #Time complexity: O(n)
        #Space complexity (ignoring input): O(1)
        sorted_array = [""]*101
        for index in range(0, len(arr)):
            sorted_index = int(arr[index][0])
            if index < len(arr)/2:
                sorted_array[sorted_index] += " -"
            else:
                sorted_array[sorted_index] += " "
                for letter in arr[index][1]:
                    sorted_array[sorted_index] += letter
    
        result_string = ""
        for string in sorted_array:
            result_string += string
    
        print(result_string[1:])
    
  • + 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))