We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Counting Sort 1
- Discussions
Counting Sort 1
Counting Sort 1
Sort by
recency
|
694 Discussions
|
Please Login in order to post a comment
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;
}
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.
Scala solution
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