Counting Sort 2

  • + 0 comments

    Java:

    public static List<Integer> countingSort(List<Integer> arr) {
      int[] freqArray = new int[100];
      List<Integer> sortedList = new ArrayList<>();
      for (int num : arr) {
        freqArray[num]++;
      }
    
      for (int i = 0; i < freqArray.length; i++) {
        for (int j = 0; j < freqArray[i]; j++) {
          sortedList.add(i);
        }
      }
    
      return sortedList;
    }