标签:
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on certain characteristic order, thus
partially sorting them in the first go.
Then depending on the number of entities in each bucket, it employs either bucket sort again or some other ad hoc sort.
BUCKET SORT (A)
1. n ← length [A]
2. For i = 1 to n do
3. Insert A[i] into list B[A[i]/b] where b is the bucket size
4. For i = 0 to n-1 do
5. Sort list B with Insertion sort
6. Concatenate the lists B[0], B[1], . . B[n-1] together in order.
Assumptions :
1 #include<iostream> 2 #include<ctime> 3 #include <stdio.h> 4 #include<cstring> 5 #include<cstdlib> 6 #include <map> 7 #include <string> 8 using namespace std; 9 void Bucket_Sort ( int array[], int n) 10 { 11 int i, j; 12 int count[n]; 13 for (i=0; i < n; i++) 14 { 15 count[i] = 0; 16 } 17 for (i=0; i < n; i++) 18 { 19 (count[array[i]])++; 20 } 21 for (i=0,j=0; i < n; i++) 22 { 23 for (; count[i]>0;(count[i])--) 24 { 25 array[j++] = i; 26 } 27 } 28 } 29 30 #if TEST 31 int main(){ 32 int arr[] = {170, 45, 75, 90, 802, 24, 2, 66}; 33 int n = sizeof(arr)/sizeof(arr[0]); 34 //radixsort(arr, n); 35 Bucket_Sort(arr, n); 36 print(arr, n); 37 } 38 #endif
标签:
原文地址:http://www.cnblogs.com/guxuanqing/p/5851484.html