The Full Counting Sort

Sort by

recency

|

81 Discussions

|

  • + 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))
    
  • + 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))