forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_sort.java
More file actions
38 lines (33 loc) · 1023 Bytes
/
counting_sort.java
File metadata and controls
38 lines (33 loc) · 1023 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
38
import java.util.*;
public class CountingSort{
//O(nlogn)
public static void main(String[] args){
int [] data= {10,23,53,22,1,1,100,32,58,34,42,64};
countingSort(data);
System.out.println(Arrays.toString(data));
}
static int[] countingSort(int[] a) {
int max = a[0], min = a[0];
for (int i = 1; i < a.length; i++)
{
if (a[i] > max)
max = a[i];
if (a[i] < min)
min = a[i];
}
int range = max - min + 1;
int[] count = new int[range];
/** make count/frequency array for each element **/
for (int i = 0; i < a.length; i++)
count[a[i] - min]++;
/** modify count so that positions in final array is obtained **/
for (int i = 1; i < range; i++)
count[i] += count[i - 1];
/** modify original array **/
int j = 0;
for (int i = 0; i < range; i++)
while (j < count[i])
a[j++] = i + min;
return a;
}
}