forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.java
More file actions
47 lines (44 loc) · 1.14 KB
/
Copy pathCountSort.java
File metadata and controls
47 lines (44 loc) · 1.14 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
import java.util.Scanner;
public class CountSort {
static void countSort(int a[],int n,int max)
{
int c[] = new int[max];
int b[] = new int[n];
for(int i=0;i<max;i++)
{
c[i]=0;
}
for(int i=0;i<n;i++)
{
c[a[i]-1]++;
}
for(int i=1;i<max;i++)
{
c[i]=c[i]+c[i-1];
}
for(int i=n-1;i>=0;i--)
{
b[c[a[i]-1]-1] = a[i];
c[a[i]-1]--;
}
System.out.println("Sorted Array:");
for(int i=0;i<n;i++)
{
System.out.println(b[i] + " ");
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter no of elements in the array: ");
int n=sc.nextInt();
int a[] = new int[n];
int max = Integer.MIN_VALUE;
System.out.println("Enter the elements of the array:");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
max=Math.max(max,a[i]);
}
countSort(a,n,max);
}
}