-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCountingSort.java
32 lines (30 loc) · 934 Bytes
/
CountingSort.java
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
package SummerTrainingGFG.Sorting;
import java.util.Arrays;
/**
* @author Vishal Singh
* - Works in linear time and is used for only small array
* Complexity = O(n+k)
* n = number of elements in the array
* k = largest element in the array + 1*/
public class CountingSort {
static void countingSort(int[] arr,int n,int k){
int[] count = new int[k];
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
for (int i = 1; i < k; i++) {
count[i] += count[i-1];
}
int[] output = new int[n];
for (int i = n-1; i >= 0; i--) {
output[count[arr[i]]-1] = arr[i];
count[arr[i]]--;
}
if (n >= 0) System.arraycopy(output, 0, arr, 0, n);
}
public static void main(String[] args) {
int[] arr = {1,4,4,1,0,1};
countingSort(arr,arr.length,5);
System.out.println(Arrays.toString(arr));
}
}