FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
competitive-programming/Sorting/BucketSort.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
/
BucketSort.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (41 loc) · 1.1 KB
Breadcrumbs
competitive-programming
/
Sorting
/
BucketSort.cpp
Copy path
File metadata and controls
47 lines (41 loc) · 1.1 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
#
include
<
iostream
>
#
include
<
algorithm
>
#
include
<
vector
>
using
namespace
std
;
void
BucketSort
(vector<
float
> &array )
//
function to sort the float array by bucket sort
{
int
n=array.
size
();
vector<
float
> b[n];
//
Creates n empty buckets
for
(
int
i=
0
;i<n;++i)
//
Categorizes the elements into different buckets
{
int
z=n*array[i];
b[z].
push_back
(array[i]);
}
for
(
int
i=
0
;i<n;++i)
{
sort
(b[i].
begin
(),b[i].
end
());
//
Sorts the invidual buckets
}
int
cursor=
0
;
for
(
int
i=
0
;i<n;++i)
{
for
(
int
j=
0
;j<b[i].
size
();++j)
{
array[cursor]=b[i][j];
//
Merges back all the sorted buckets
cursor++;
}
}
}
int
main
()
{
float
arr[] = {
0.697
,
0.765
,
0.456
,
0.9234
,
0.865
,
0.2434
};
//
array is predefined here, please take input from the user to work accordingly by modifying the code
int
n =
sizeof
(arr) /
sizeof
(arr[
0
]);
vector<
float
>
ar
(arr,arr+n);
BucketSort
(ar);
cout<<
"
Float Array after sorting is:
\n
"
;
for
(
int
i=
0
;i<n;++i)
{
cout<<ar[i]<<
"
"
;
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL