Implement Counting Sort. It works by counting the occurrences of each distinct element, then using arithmetic to determine the positions of each element in the output sequence.

Input: [4,2,2,8,3,3,1] → Output: [1,2,2,3,3,4,8]Input: [1,0,3,1,3,1] → Output: [0,1,1,1,3,3]

Count frequency of each element (given a known range 0..k). Compute prefix sums to get positions. Place each element in its correct output position.

import java.util.Arrays; class CountingSort { public int[] sort(int[] arr) { if (arr.length == 0) return arr; int max = Arrays.stream(arr).max().getAsInt(); int min = Arrays.stream(arr).min().getAsInt(); int range = max - min + 1; int[] count = new int[range]; int[] output = new int[arr.length]; for (int v : arr) count[v - min]++; for (int i = 1; i < range; i++) count[i] += count[i - 1]; for (int i = arr.length - 1; i >= 0; i--) { output[--count[arr[i] - min]] = arr[i]; } return output; } }