首页 > 代码库 > 桶排序(bucket sort)
桶排序(bucket sort)
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 TEST31 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
桶排序(bucket sort)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。