forked from JGEC-Winter-of-Code/JWoC_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingSort.java
37 lines (37 loc) · 1.07 KB
/
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
33
34
35
36
37
import java.util.*;
class CountingSort
{
void sort(char arr[])
{
int n = arr.length;
char output[] = new char[n];
int count[] = new int[256];
for (int i=0; i<256; ++i)
count[i] = 0;
for (int i=0; i<n; ++i)
++count[arr[i]];
for (int i=1; i<=255; ++i)
count[i] += count[i-1];
for (int i = n-1; i>=0; i--)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
for (int i = 0; i<n; ++i)
arr[i] = output[i];
}
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of the string:");
int l=sc.nextInt();
char arr[] =new char[l];
System.out.println("Enter a string(one character at a time):");
for(int i=0;i<l;i++)
arr[i] = sc.next().charAt(0);
sort(arr);
System.out.print("Sorted character array is :");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i]);
}
}