added bucket & counting sort in cpp (#57)

* add binary search in js

* added merge sort in c

* added quick sort in cpp

* modified quick sort

* added heap sort

* updated readme.md

* added counting sort

* added bucket sort
pull/60/head
Satyam Singh 2021-02-03 20:35:35 +05:30 committed by GitHub
parent 455bb11a96
commit bdfa4e1576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -9,6 +9,7 @@
5. [Quick Sort](c-or-cpp/quick-sort.cpp) 5. [Quick Sort](c-or-cpp/quick-sort.cpp)
6. [Heap Sort](c-or-cpp/heap-sort.cpp) 6. [Heap Sort](c-or-cpp/heap-sort.cpp)
7. [Counting Sort](c-or-cpp/counting-sort.cpp) 7. [Counting Sort](c-or-cpp/counting-sort.cpp)
8. [Bucket Sort](c-or-cpp/bucket-sort.cpp)
### C# ### C#

View File

@ -0,0 +1,40 @@
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void bucketSort(float a[], int n);
// Driver Code
int main()
{
float a[] = {0.971, 0.565, 0.420, 0.123, 0.315, 0.696};
int n = sizeof(a) / sizeof(a[0]);
cout << "Original array:\n";
for (int i = 0; i < n; i++)
cout << a[i] << " ";
bucketSort(a, n);
cout << "\nSorted array:\n";
for (int i = 0; i < n; i++)
cout << a[i] << " ";
return 0;
}
void bucketSort(float a[], int n)
{
// Create n empty buckets
vector<float> b[n];
//Put array elements in different buckets
for (int i = 0; i < n; i++)
{
int ib = n * a[i]; // Index in bucket
b[ib].push_back(a[i]);
}
//Sort individual buckets
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());
//Concatenate all buckets into a[]
int in = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
a[in++] = b[i][j];
}