Counting Sort 1

Sort by

recency

|

696 Discussions

|

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

    Java

    List<Integer> frequencies = new ArrayList<>();
    for(int i =0; i < 100; i++){
    		frequencies.add(Collections.frequency(arr, i));
    }
    return frequencies;
    
  • + 0 comments

    Java 8:

    • public static List countingSort(List arr) { Map countMap = new HashMap<>(); List result = new ArrayList<>(); for (int i = 0; i< 100; i++) { countMap.put(i, 0); }

      for (int num : arr) { int value = countMap.get(num); countMap.put(num, value + 1); }

      for (int i = 0; i < 100; i++) { result.add(countMap.get(i)); }

      return result;

      }

  • + 0 comments

    In the question description, the input format is as follows:

    Input Format

    The first line contains an integer , the number of items in . Each of the next lines contains an integer where .

    If you solve it with C++, its input is a vector and the size() tells you what N is.

    So there is no first line input.

  • + 0 comments

    Scala solution

    def countingSort(arr: Array[Int]): Array[Int] = {
        // Write your code here
            val frequency = Array.fill(100)(0)
            for (num <- arr) {
            frequency(num) += 1
            }
            frequency
        }