-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
37 lines (28 loc) · 1015 Bytes
/
Copy pathCountingSort.java
File metadata and controls
37 lines (28 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Arrays;
public class CountingSort {
public static void countingSort(int[] array, int max) {
int[] countArray = new int[max + 1];
int[] sortedArray = new int[array.length];
for (int num : array) {
countArray[num]++;
}
for (int i = 1; i < countArray.length; i++) {
countArray[i] += countArray[i - 1];
}
for (int i = array.length - 1; i >= 0; i--) {
sortedArray[countArray[array[i]] - 1] = array[i];
countArray[array[i]]--;
}
System.arraycopy(sortedArray, 0, array, 0, array.length);
}
public static void main(String[] args) {
int[] arr = {3, 69, 45, 88, 24, 1, 5};
int max = 88;
System.out.println("Original array: ");
System.out.print(Arrays.toString(arr));
System.out.println();
countingSort(arr, max);
System.out.println("Sorted array: ");
System.out.print(Arrays.toString(arr));
}
}