Counting Sort 2

  • + 0 comments

    My Java solution with o(n) time complexity and o(n) space complexity:

    public static List<Integer> countingSort(List<Integer> arr) {
            int[] freqArr = new int[100];
            arr.forEach(num -> freqArr[num]++);
    
            return IntStream.range(0, 100)
                .boxed()
                .flatMap(i -> Collections.nCopies(freqArr[i], i).stream())
                .collect(Collectors.toList());
        }