Counting Sort 1

Sort by

recency

|

677 Discussions

|

  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    def countingSort(arr, n): count = [0] * 100 # Fixed-size array of 100 elements

    for num in arr:
        count[num] += 1  # Count occurrences efficiently
    
    return count
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())
    
    arr = list(map(int, input().rstrip().split()))
    
    result = countingSort(arr,n)
    
    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')
    
    fptr.close()
    
  • + 0 comments

    its shows error what to do?

    int* countingSort(int arr_count, int* arr, int* result_count) { for(int i=0;i<100;i++){ result_count[i]=0; } for(int i=0;i= 0 && arr[i] < 100) { result_count[arr[i]]++;} } return result_count;

    }

  • + 0 comments

    c# solution:

    public static List countingSort(List arr) { var zeroArray = new int[100];

        for (var i = 0; i < arr.Count; i++)
        {
            zeroArray[arr[i]]++;
        }
        return zeroArray.ToList();
    }
    
  • + 2 comments

    I did not understand the problem, can someone explain it to me?

    • + 0 comments

      Create new array which size is 100 because maximum capacitiy can be 100 as written in document.

      With for loop, check the every index of given input array. For example if first element is 23, it means go to index 23 of newly created array and increase one (+1).

    • + 0 comments

      Create new array which size is 100 because maximum capacitiy can be 100 as written in document.

      With for loop, check the every index of given input array. For example if first element is 23, it means go to index 23 of newly created array and increase one (+1).

  • + 0 comments
    func countingSort(arr []int32) []int32 {
    	result := make([]int32, 100)
    	for _, num := range arr {
    		result[num]++
    	}
    	return result
    }