The Full Counting Sort

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