| 2 | import java.util.Arrays; |
| 3 | |
| 4 | public class CountingSortMain { |
| 5 | |
| 6 | public static void main(String[] args) { |
| 7 | System.out.println("Before Sorting : "); |
| 8 | int arr[]={1,4,7,3,4,5,6,3,4,8,6,4,4}; |
| 9 | System.out.println(Arrays.toString(arr)); |
| 10 | arr=countingSort(arr); |
| 11 | System.out.println("======================="); |
| 12 | System.out.println("After Sorting : "); |
| 13 | System.out.println(Arrays.toString(arr)); |
| 14 | } |
| 15 | |
| 16 | static int[] countingSort(int arr[]) |
| 17 | { |
| 18 | int n = arr.length; |
| 19 | |
| 20 | int result[] = new int[n]; |
| 21 | |
| 22 | int count[] = new int[9]; |
| 23 | for (int i=0; i<9; ++i) |
| 24 | count[i] = 0; |
| 25 | |
| 26 | for (int i=0; i<n; ++i) |
| 27 | ++count[arr[i]]; |
| 28 | |
| 29 | for (int i=1; i<=8; ++i) |
| 30 | count[i] += count[i-1]; |
| 31 | |
| 32 | for (int i = 0; i<n; ++i) |
| 33 | { |
| 34 | result[count[arr[i]]-1] = arr[i]; |
| 35 | --count[arr[i]]; |
| 36 | } |
| 37 | |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | } |
nothing calls this directly
no outgoing calls
no test coverage detected