Counting Sort 1

Sort by

recency

|

649 Discussions

|

  • + 0 comments

    in python: * def countingSort(arr): * # Write your code here * result = [0]*100 * for i in range(len(arr)): * result[arr[i]]+= 1 * return result

  • + 0 comments

    JS Solution:

    function countingSort(arr) { return Object.values( arr.reduce((acc, cur) => { acc[cur] = (acc[cur] || 0) + 1; return acc; }, new Array(100).fill(0)) ); }

  • + 0 comments
    def countingsort(arr): 
    		res=[0]*100 
    		for i in arr: 
    			res[i]+=1 
    		return res 
    def countingsort(arr): 
    		return [arr.count(i) for i in range(100)] 
    

    First one is better cause you don't have to count the number with zero freq.

  • + 0 comments

    js single line solution:

    const countingSort = (arr) => (arr.reduce((freqArr, num) => (freqArr[num]=++freqArr[num])&&freqArr, Array(100).fill(0)));
    
  • + 0 comments
    def countingSort(arr):
        # create an array of zeros with the length (100)
        result = [0 for i in range(100)]
        # iterate the array and add one to the index everytime it see the same number as the index
        for i in arr:
            result[i] = result[i] + 1
        print(result)
        return result