Repository navigation
Expand file tree
/
Copy pathcountingSort.java
More file actions
51 lines (46 loc) · 1.5 KB
/
Copy pathcountingSort.java
File metadata and controls
51 lines (46 loc) · 1.5 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Arrays;
import java.util.Collections;
public class countingSort {
public static void performCountingSort(int arr[], int n) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
// Getting max value and creating count array
for(int i = 0; i < n; i++)
max = Math.max(max, arr[i]);
for(int i = 0; i < n; i++)
min = Math.min(min, arr[i]);
int count[] = new int[max + 1];
// Filling array with zeroes
for (int i = 0; i < count.length; i++)
count[i] = 0;
// Putting frequencies into count
for (int i = 0; i < n; i++)
count[arr[i]]++;
// Sorting
int j = 0; // index for inserting into arr
for (int i = 0; i < count.length; i++) {
while (count[i] != 0) {
arr[j] = i;
j++;
count[i]--;
}
}
}
public static void displayArray(int arr[], int n) {
for(int i=0; i<n; i++) {
if (i == n-1)
System.out.print(arr[i] + " ");
else
System.out.print(arr[i] + ", ");
}
}
public static void main (String[] args) {
int arr[] = {5, 4, 1, 2, 3};
System.out.print("Original Array: ");
displayArray(arr, arr.length);
System.out.println();
performCountingSort(arr, arr.length);
System.out.print("Counting Sorted Array: ");
displayArray(arr, arr.length);
}
}