Bucket sort ( bucket sort) or so-called box sorting is a sort algorithm that works by splitting an array into a finite number of buckets. Each bucket is sorted separately (it is possible to use a different sorting algorithm or recursively continue to sort by using the bucket sort). Bucket sequencing is an inductive result of pigeon nest sorting. When the values in the array to be sorted are evenly distributed, the bucket is sorted using linear time (Θ (n)). But the bucket sort is not a comparison sort, and he is not affected by the O (n log n) lower bound.
This article address: http://www.cnblogs.com/archimedes/p/bucket-sort-algorithm.html, reprint please indicate source address.
The buckets are sorted by the following procedure:
Sets a quantitative array as an empty bucket.
Search the sequence, and put the project one to the corresponding bucket.
Sorts each bucket that is not empty.
From a bucket that is not empty, put the item back in the original sequence.
Pseudo Code
is newarray of n Empty lists for0 to (length (array)-1 do insert array[i] to Buckets[msbits (Array[i], K)] for0 1 Do Next-sort (buckets[i]) return the concatenation of buckets[0 ],..., buckets[n-1]
Algorithm implementation
//completed on 2014.10.13 12:20//Language:c99////Copyright (C) Codingwu (mail: [email protected])//Blog Address:http://www.cnblogs.com/archimedes/#include <stdio.h>#include<stdlib.h>typedefstructNode {intvalue; structNode *Next;} node,*Pnode;voidBucketsort (intA[], node bucket[],intN) { inti =0; Pnode tmp, T; for(i =0; I < n; i++) {tmp= &bucket[a[i]/Ten]; T= (Pnode) malloc (sizeof(node)); T->value =A[i]; T->next =NULL; while(TMP! =NULL) { if(Tmp->next = =NULL) {T->next = tmp->Next; TMP->next =T; Break; } Else if(Tmp->next->value > t->value) {T->next = tmp->Next; TMP->next =T; Break; } Else{tmp= tmp->Next; } } }}intMain () {node bucket[Ten]; Pnode tmp; intA[] = { A, A, the, -, -, About, the, the, the, -, $, About}; intSize =sizeof(a)/sizeof(a[0]); for(inti =0; I <Ten; i++) Bucket[i].next=NULL; Bucketsort (A, bucket, size); for(intK =0; K <Ten; k++) {tmp=Bucket[k].next; while(TMP! =NULL) {printf ("%d", tmp->value); TMP= tmp->Next; }} printf ("\ n"); return 0;}
Bucket sorting algorithm