Bucket sorting algorithm

Source: Internet
Author: User

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:

    1. Sets a quantitative array as an empty bucket.

    2. Search the sequence, and put the project one to the corresponding bucket.

    3. Sorts each bucket that is not empty.

    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.