Counting Sort 1

Sort by

recency

|

694 Discussions

|

  • + 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
        }
    
  • + 0 comments
     int totalCount = arr.Count;
    
     List<int> a = new List<int>(totalCount);
     for(int i =0;i< 100;i++)
     {
         a.Add(0);
     }
     int count = 0;
     for (int i = 0; i < arr.Count; i++)
     {
         if (a[arr[i]] > 0)
         {
             a[arr[i]] =++a[arr[i]];
         }
         else
         {
             a[arr[i]] = ++count;
         }
         count = 0;
     }
    
  • + 0 comments

    why its not working for 5th test case def countingSort(arr): arr.sort() d=arr[-1] l=[] for i in range(d+1): count=0 for j in arr: if i==j: count+=1 l.append(count) return l