-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathQ1Count.java
56 lines (53 loc) · 1.21 KB
/
Q1Count.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
class Count
{
void countingSort(int[] A, int[] B, int n)
{
int r = A[0];
for(int i = 1; i < n; ++i)
{
if(A[i] > r)
{
r = A[i];
}
}
int C[] = new int[r + 1];
for(int i = 0; i < r + 1; ++i)
{
C[i] = 0;
}
for(int i = 0; i < n; ++i)
{
C[A[i]] += 1;
}
for(int i = 1; i < r + 1; ++i)
{
C[i] += C[i - 1];
}
for(int i = n - 1; i >= 0; --i)
{
B[C[A[i]] - 1] = A[i];
C[A[i]] -= 1;
}
}
}
class Q1Count
{
public static void main(String[] args)
{
Count obj = new Count();
Random rand = new Random();
int n = 10000;
int A[] = new int[n];
int B[] = new int[n];
for(int i = 0; i < n; ++i)
{
A[i] = rand.nextInt(100);
}
long start_time, end_time;
start_time = System.nanoTime();
obj.countingSort(A, B, n);
end_time = System.nanoTime();
System.out.println(end_time - start_time);
}
}