forked from jaspreetkaur96/CodingQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_sort
More file actions
26 lines (22 loc) · 743 Bytes
/
count_sort
File metadata and controls
26 lines (22 loc) · 743 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
public static void countSort(char seq[])
{
int count[] = new int[26];
char result [] = new char[seq.length];
for(int i = 0 ; i < seq.length ; i++)
{
count[(int)seq[i] - 'a'] += 1;
}
for(int i = 1 ; i < count.length ; i++)
{
count[i] += count[i-1];
}
for(int i = 0 ; i < seq.length ; i++)
{
result[--count[(int)seq[i] - 'a']] = seq[i];
// System.out.print(result[count[(int)seq[i] - 'a']]);
// System.out.print(count[(int)seq[i] - 'a']);
}
for(int i = 0 ; i < seq.length ; i++)
{
seq[i] = result[i];
}