forked from thuva4/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 (31 loc) · 1.12 KB
/
counting_sort.java
File metadata and controls
38 lines (31 loc) · 1.12 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
import java.util.Scanner;
class GFG
{
public static void counting_sort(int a[], int range)
{
int c[] = new int[range]; // declare array for keeping count (frequency)
for(int i = 1; i< range; i++) // initialise them to 0
c[i] = 0;
for(int i = 0; i<a.length; i++) // count the frequency of each number in range
c[a[i]]++;
int k=0;
for(int i = 0; i< range; i++) // for each number in range
for(int j = 0; j<c[i]; j++) // add the number as many times as its frequency
a[k++] = i;
}
public static void main (String[] args)
{
int n,range;
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); // number of elements to be sorted
range = sc.nextInt(); // range of the elements
int a[] = new int[n];
//read the list to be sorted
for(int i = 0; i<n; i++)
a[i] = sc.nextInt();
counting_sort(a,range); // perform counting sort
// display the elements after sorting
for(int i = 0; i<n; i++)
System.out.print(a[i] + " ");
}
}