diff --git a/sorting/README.md b/sorting/README.md index 8ab3d178..63a1761f 100644 --- a/sorting/README.md +++ b/sorting/README.md @@ -37,6 +37,7 @@ 3. [Selection Sort](java/selection-sort.java) 4. [Merge Sort](java/merge-sort.java) 5. [Quick Sort](java/quick-sort.java) +6. [Counting Sort](java/counting-sort.java) ### JavaScript diff --git a/sorting/java/counting-sort.java b/sorting/java/counting-sort.java new file mode 100644 index 00000000..e4eacea2 --- /dev/null +++ b/sorting/java/counting-sort.java @@ -0,0 +1,74 @@ +public class CountingSort { + + /** + * Public method to use the counting sort algortithm + * @param arr - the array in input + */ + public void sort(int[] arr){ + + int[] sortedArray = new int[arr.length + 1]; + + // Search max element + int max = arr[0]; + for(int i = 0; i < arr.length; i++){ + if(arr[i] > max){ + max = arr[i]; + } + } + + // Create counting array + int[] countArray = new int[max + 1]; + for(int i = 0; i < countArray.length; i++){ + countArray[i] = 0; + } + + // Count elements in array + for(int i = 0; i < arr.length; i++){ + countArray[arr[i]]++; + } + + + for(int i = 1; i < countArray.length; i++){ + countArray[i] += countArray[i - 1]; + } + + for (int i = arr.length - 1; i >= 0; i--) { + sortedArray[countArray[arr[i]] - 1] = arr[i]; + countArray[arr[i]]--; + } + + System.arraycopy(sortedArray, 0, arr, 0, arr.length); + } + + /** + * Main method for testing + * @param args + */ + public static void main(String[] args) throws Exception { + CountingSort count = new CountingSort(); + + int[] test = {1,9,8,7,6,5,4,3,2,12,546,23,123,5768,689,45,6342,0,76,856,34,412,12,32,353,46,568,456,234,3456,467,345345,345,345,}; + + count.sort(test); + + for(int i = 1; i < test.length; i++){ + if(test[i] < test[i - 1]){ + throw new Exception("[ERROR] Ops! Counting sort not work!"); + } + } + + printArray(test); + } + + /** + * Print an array + * @param arr + */ + public static void printArray(int[] arr){ + + System.out.println("Sorted array: "); + for(int i = 0; i < arr.length; i++){ + System.out.println(" " + i); + } + } +}