FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structures-Algorithms/Sorting/CountingSort.cpp at master · KalshuCodes/Data-Structures-Algorithms · GitHub
KalshuCodes
/
Data-Structures-Algorithms
Public
forked from
CodersForLife/Data-Structures-Algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Data-Structures-Algorithms
/
Sorting
/
CountingSort.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (42 loc) · 1.09 KB
Breadcrumbs
Data-Structures-Algorithms
/
Sorting
/
CountingSort.cpp
Copy path
File metadata and controls
43 lines (42 loc) · 1.09 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
#
include
<
bits/stdc++.h
>
//
including all the libraries of cpp
using
namespace
std
;
int
main
()
{
int
N;
cout<<
"
Enter the number of elements to sort
"
<<endl;
cin>>N;
//
to enter the number of elements to sort
int
a[N];
//
array to store all the elements
cout<<
"
Start entering the numbers
"
<<endl;
for
(
int
i =
0
; i < N; ++i)
//
loop to take input of the elements
{
cin>>a[i];
}
int
min,max;
//
variables to store the max and min values
min=max=a[
0
];
for
(
int
i=
0
;i<N;i++)
//
loop to find the max and min values
{
if
(a[i]<min)
min=a[i];
else
if
(a[i]>max)
max=a[i];
}
int
sort[max-min+
1
];
//
array to count the frequency of all elements
for
(
int
i=min;i<=max;i++)
//
initialising all frequencies to zero
{
sort[i]=
0
;
}
for
(
int
i=
0
;i<N;i++)
//
increasing the count of each element
{
sort[a[i]-min]++;
}
cout<<
"
Sorted array using counting sort:
"
<<endl;
for
(
int
i=min;i<=max;i++)
//
loop to print the elements in increasing order
{
for
(
int
j=
0
;j<sort[i];j++)
//
loop to print each element equal to the times of it's occurence
{
cout<<i<<
"
"
;
}
}
cout<<endl;
return
0
;
}
//
end
Back
|
FazBrowse Home
|
New Git URL