public class CountingSort {
	public static int[] countingSort(int[] unsorted) {
		int maxValue = max(unsorted);

		// Count occurrences of each value in unsorted
		int[] counts = new int[maxValue + 1];

		for(int value : unsorted)
			counts[value]++;

		for(int i = 1; i < counts.length; i++)
			counts[i] += counts[i - 1];

		// Reorder values in unsorted
		int[] sorted = new int[unsorted.length];
		for(int value : unsorted) {
			counts[value] -= 1;
			int index = counts[value];
			sorted[index] = value;
		}

		return sorted;
	}

	private static int max(int[] array) {
		int max = Integer.MIN_VALUE;

		for(int value : array) {
			if(value > max) max = value;
		}

		return max;
	}

	public static void main(String[] args) {
		int[] unsorted = {5, 5, 5, 4, 3, 2, 1, 0};
		int[] sorted = countingSort(unsorted);
		for(int value : sorted)
			System.out.println(value);
	}
}
@@ -84,31 +84,169 @@
 nt m
-axValue = max(unsorted)
+inValue = min(unsorted);%0A%09%09int maxValue = max(unsorted);%0A%0A%09%09// Adjust max so that min equals zero%0A%09%09int offset = -minValue;%0A%09%09int adjustedMax = maxValue + offset
 ;%0A%0A%09
@@ -318,24 +318,27 @@
 new int%5B
-maxValue
+adjustedMax
  + 1%5D;%0A%0A
@@ -380,16 +380,25 @@
 ts%5Bvalue
+ + offset
 %5D++;%0A%0A%09%09
@@ -577,31 +577,8 @@
 ) %7B%0A
-%09%09%09counts%5Bvalue%5D -= 1;%0A
 %09%09%09i
@@ -588,16 +588,18 @@
 index = 
+--
 counts%5Bv
@@ -602,16 +602,25 @@
 ts%5Bvalue
+ + offset
 %5D;%0A%09%09%09so
@@ -666,16 +666,163 @@
 ed;%0A%09%7D%0A%0A
+%09private static int min(int%5B%5D array) %7B%0A%09%09int min = Integer.MAX_VALUE;%0A%0A%09%09for(int value : array)%0A%09%09%09if(value %3C min) min = value;%0A%0A%09%09return min;%0A%09%7D%0A%0A
 %09private
@@ -908,18 +908,16 @@
 : array)
- %7B
 %0A%09%09%09if(v
@@ -932,36 +932,32 @@
 ax) max = value;
-%0A%09%09%7D
 %0A%0A%09%09return max;%0A
@@ -1044,16 +1044,36 @@
  2, 1, 0
+, -1, -2, -3, -3, -3
 %7D;%0A%09%09int

Allow counting sort to handle negative numbers