forked from Sireejaa16/Assignment-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket_sort.cpp
More file actions
79 lines (66 loc) · 1.95 KB
/
bucket_sort.cpp
File metadata and controls
79 lines (66 loc) · 1.95 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// C++ program to sort an array of positive
// and negative numbers using bucket sort
#include <bits/stdc++.h>
using namespace std;
// Function to sort arr[] of size n using
// bucket sort
void bucketSort(vector<float> &arr, int n)
{
// 1) Create n empty buckets
vector<float> b[n];
// 2) Put array elements in different
// buckets
for (int i=0; i<n; i++)
{
int bi = n*arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}
// 3) Sort individual buckets
for (int i=0; i<n; i++)
sort(b[i].begin(), b[i].end());
// 4) Concatenate all buckets into arr[]
int index = 0;
arr.clear();
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr.push_back(b[i][j]);
}
// This function mainly slpits array into two
// and then calls bucketSort() for two arrays.
void sortMixed(float arr[], int n)
{
vector<float>Neg ;
vector<float>Pos;
// traverse array elements
for (int i=0; i<n; i++)
{
if (arr[i] < 0)
// store -Ve elements by
// converting into +ve element
Neg.push_back (-1 * arr[i]) ;
else
// store +ve elements
Pos.push_back (arr[i]) ;
}
bucketSort(Neg, (int)Neg.size());
bucketSort(Pos, (int)Pos.size());
// First store elements of Neg[] array
// by converting into -ve
for (int i=0; i < Neg.size(); i++)
arr[i] = -1 * Neg[ Neg.size() -1 - i];
// store +ve element
for(int j=Neg.size(); j < n; j++)
arr[j] = Pos[j - Neg.size()];
}
/* Driver program to test above function */
int main()
{
float arr[] = {-0.897, 0.565, 0.656,
-0.1234, 0, 0.3434};
int n = sizeof(arr)/sizeof(arr[0]);
sortMixed(arr, n);
cout << "Sorted array is \n";
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}