Bucket sequencing and C language implementation

Source: Internet
Author: User

This article mainly refer to this blog: http://hxraid.iteye.com/blog/647759

The basic principle of the bucket sorting algorithm is that all the elements in the array are divided into chunks, that is, several buckets, then the data in each bucket is sorted, and then all the data in the bucket is arranged sequentially.

There are two questions here:

(1) How to divide the data block, that is, a few barrels, the number of each bucket to put the data.

(2) How to sort the data in each data block.

For the first question: Use the function mapping method to divide. As a simple example, assuming that the data are all 2-bit positive integers, the corresponding mapping function can be f (x) =x/10. That is, the ten same numbers are zoned to the same data block. As shown in.

For the second question: sort the elements of each block using the previously mentioned sort algorithm, such as insert sort, quick sort, etc.

Similarly, the analysis of the time complexity of the algorithm is based on the effects of the above two problems. The data block is divided into arrays, the time complexity is O (n), each data block is sorted, and the time complexity depends on the sorting algorithm used. In the extreme case, there is only one data per bucket, so the complexity of the time is undoubtedly optimal, since each block of data does not need to be sorted. But the complexity of the space is too high.

In short, the idea of bucket sequencing is to increase the complexity of time by dividing all the elements in an array into blocks of data and sorting each chunk separately. The algorithm is mainly suitable for the situations where the quantity is relatively large and the number is comparatively concentrated. Here is the code.

#include <stdio.h> #include <stdlib.h>typedef struct Node {int key;struct node *next;} Keynode;void bucket_sort (int keys[],int size,int bucket_size) {int i,j; Keynode **bucket_table = (Keynode *) malloc (bucket_size * sizeof (keynode*)); for (i = 0;i < bucket_size;i++) {Bucket_tab Le[i] = (keynode*) malloc (sizeof (Keynode)); Bucket_table[i]->key = 0;bucket_table[i]->next = NULL;} for (j = 0;j < size;j++) {Keynode *node = (Keynode *) malloc (sizeof (Keynode)); Node->key = Keys[j];node->next = NULL ; int index = KEYS[J]/10; Keynode *p = bucket_table[index];if (P->key = = 0) {bucket_table[index]->next = node; (bucket_table[index]->key) ++;} else {while (P->next! = NULL && p->next->key <= node->key) p = P->next;node->next = P->next ;p->next = node; (bucket_table[index]->key) + +;}} Print Resultkeynode * k = null;for (i = 0;i < bucket_size;i++) for (k = bucket_table[i]->next;k!=null;k=k->next) p rintf ("%d", K->key);p rintf ("\ n");} int main() {int raw[] = {49,38,65,97,76,13,27,49};int size = sizeof (RAW)/sizeof (int); Bucket_sort (raw,size,10);} 

Bucket sequencing and C language implementation

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.