This is my first essay, in order to share the learning experience, and we discuss some algorithms, so as to achieve a little progress, but also a summary of learning.
Words do not say much, I will use the text and graphics to introduce you to the bucket sort.
1. Main ideas:
The general idea of the bucket sorting is to first divide the array into a finite bucket, and then sort the data in each bucket, which can be said to be an inductive result of the sort of pigeon nests (the sort of data in each bucket can be a recursive, or other algorithm, in which the data in the bucket is used with the best insertion sort).
2, algorithm efficiency:
The time complexity for bucket ordering of n data is divided into two parts:
1, the calculation of each data mapping function (mapping function determines which bucket the data will be divided), the time complexity of O (N).
2, the sequencing of the data in the bucket, the time complexity is ∑o (Ni*logni), where Ni is the volume of the first bucket of data.
For N rows of data, M buckets, the average time complexity of the bucket ordering for each bucket [n/m] data is: O (N) +o (m* (n/m) *log (n/m)) =o (n+n* (LOGN-LOGM)) =o (N+N*LOGN-N*LOGM), when N=m, That is, when there is only one data per bucket in the extreme case. The best efficiency of bucket sequencing can be achieved by O (N).
For the same amount of data, the greater the number of buckets, the more evenly dispersed the data, the higher the efficiency of the barrel sequencing, the more efficient the barrel sequencing is, the more the cost of the space is to be replaced.
3, Algorithm analysis:
① Initialize the bucket:
Buckets are actually a set of pointers to pointers, a bit like the chain address method in the hash table, and the difference is that the bucket itself is a struct (Figure 1 is the chain address method, Figure 2 is the initialized bucket)
② the bucket by placing the data in the appropriate bucket:
Traversing the data, according to the mapping function to calculate the data, the mapping function is N/10 (n is the current data), after the bucket is determined, the data in the bucket using the direct insertion method. Sorts the buckets of array A.
(Medium key is the number of data in the bucket)
Take 25 and 23 for example, 25/10=2, determine 25 position in the second bucket, at this time the bucket 2 has no element, so directly inserted, 23/10=2, OK in the second bucket, at this time the Bucket 2 key is not 0,23<25 will 23 inserted 25 before the other similar.
③ output The elements in the order of the buckets:
According to the case, the order after sorting is: 10 23 25 26 30 41 43
4, Code display (C language):
#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 main ()
{
int a[]={11,11,9,21,14,55,77,99,53,25};
int size=sizeof (a)/sizeof (a[0]);
Bucket_sort (a,size,10);
return 0;
}
void Bucket_sort (int keys[],int size,int bucket_size)
{
Keynode **bucket_table= (keynode**) malloc (bucket_size*sizeof (keynode*));
for (int i=0;i<bucket_size;i++) {//Initialize bucket
bucket_table[i]= (keynode*) malloc (sizeof (Keynode));
bucket_table[i]->key=0;
bucket_table[i]->next=null;
}
for (int i=0;i<size;i++) {
keynode* node= (keynode*) malloc (sizeof (Keynode));
node->key=keys[i];
node->next=null;
int index=keys[i]/10;//method for classifying data (related to sort speed, very important)
Keynode *p=bucket_table[index];
if (p->key==0) {
p->next=node;
p->key++;
}
else{
while (P->next!=null&&p->next->key<=node->key) {//=, later elements will be in the back
p=p->next;
}
node->next=p->next;
p->next=node;
(Bucket_table[index]->key) + +;
}
}
keynode* K=null;
for (int i=0;i<bucket_size;i++) {
for (K=bucket_table[i]->next;k!=null;k=k->next) {
printf ("%d", k->key);
}
}
}
The sorting algorithm--bucket sort