FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
competitive-programming/Sorting/countsort.cpp at master · kothariji/competitive-programming · GitHub
kothariji
/
competitive-programming
Public
Notifications
You must be signed in to change notification settings
Fork
500
Star
704
Code
Issues
1
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
competitive-programming
/
Sorting
/
countsort.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (48 loc) · 1.36 KB
Breadcrumbs
competitive-programming
/
Sorting
/
countsort.cpp
Copy path
File metadata and controls
58 lines (48 loc) · 1.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
57
58
#
include
<
bits/stdc++.h
>
#
include
<
string.h
>
using
namespace
std
;
#
define
RANGE
255
//
The main function that sort
//
the given string arr[] in
//
alphabatical order
void
countSort
(
char
arr[])
{
//
The output character array
//
that will have sorted arr
char
output[
strlen
(arr)];
//
Create a count array to store count of inidividul
//
characters and initialize count array as 0
int
count[
RANGE
+
1
], i;
memset
(count,
0
,
sizeof
(count));
//
Store count of each character
for
(i =
0
; arr[i]; ++i)
++count[arr[i]];
//
Change count[i] so that count[i] now contains actual
//
position of this character in output array
for
(i =
1
; i <=
RANGE
; ++i)
count[i] += count[i -
1
];
//
Build the output character array
for
(i =
0
; arr[i]; ++i) {
output[count[arr[i]] -
1
] = arr[i];
--count[arr[i]];
}
/*
For Stable algorithm
for (i = sizeof(arr)-1; i>=0; --i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
*/
//
Copy the output array to arr, so that arr now
//
contains sorted characters
for
(i =
0
; arr[i]; ++i)
arr[i] = output[i];
}
int
main
()
{
char
arr[] =
"
geeksforgeeks
"
;
countSort
(arr);
cout <<
"
Sorted character array is
"
<< arr;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL