Counting Sort 1

Sort by

recency

|

133 Discussions

|

  • + 0 comments
    public static List<int> countingSort(List<int> arr)
        {
            int[] aux = new int[100]; 
            for(int j = 0; j < arr.Count; j++) aux[arr[j]]++;
            return aux.ToList();
        }
    
  • + 1 comment
    fun countingSort(arr: Array<Int>): Array<Int> {
        // Write your code here
        val result = Array(100) { 0 }
    
        for (i in 0..<arr.size) {
            result[arr[i]]++
        }
    
        return result
    }
    
  • + 0 comments
    public static List<int> countingSort(List<int> arr)
        {
            List<int> result = Enumerable.Repeat(0, 100).ToList();
            for(int i=0; i<arr.Count; i++){
                result[arr[i]]++;
            }
            return result;
        }
    
  • + 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

    pub fn counting_sort(arr: &[i32]) -> Vec<i32> {
        //Time complexity: O(n+k) or just O(n), since k is constant
        //Space complexity (ignoring input): O(k) or just O(1), since k is constant
        let mut frequency_arr = vec![0; 100];
        for &number in arr{
            frequency_arr[number as usize] += 1;
        }
    
        frequency_arr
    }
    
  • + 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 counting_sort(arr):
        #Time complexity: O(n+k) or just O(n), since k is constant
        #Space complexity (ignoring input): O(k) or just O(1), since k is constant
        frequency_arr = [0]*100
        for number in arr:
            frequency_arr[number] += 1
    
        return frequency_arr