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 sortpull/60/head
parent
455bb11a96
commit
bdfa4e1576
|
@ -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#
|
||||||
|
|
|
@ -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];
|
||||||
|
}
|
Loading…
Reference in New Issue