Introduction to algorithms-hill sorting and bucket sorting

Source: Internet
Author: User

I. Hill sorting
Basic Ideas

Take an integer D1 less than N as the first increment, and divide all records of the file into (n divided by D1) groups. All records whose distance is multiples of D1 are placed in the same group. Insert and sort data in each group first. Then, repeat the group and sort data in the second incremental D2 <d1 until the incremental dt = 1 (dt <DT-L <... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.


C language implementation:

Void shellsort (elementtype * r, int N) {Int J, I, gap; elementtype TMP; Gap = n/2; while (GAP> 0) {for (I = gap; I <n; I ++) {J = I-gap; while (j> = 0) {If (R [J]> r [J + Gap]) {TMP = R [J]; R [J] = R [J + Gap]; R [J + Gap] = TMP; // move a gap forward, continue to compare J = J-gap;} // jump out of the loop else J =-1 ;}} gap/= 2 ;}}

Algorithm Analysis
A large amount of auxiliary space is not required, which is as easy to implement as merging and sorting. Hill sorting is an algorithm based on insert sorting, which adds a new feature and improves efficiency. The time complexity of hill sorting is O (n * (logn) 2), and there is no fast Sorting Algorithm for fast O (N * (logn). Therefore, the medium-size sorting performance is good, sorting data of a very large scale is not the optimal choice. But it is much faster than the O (n2) algorithm. In addition, Hill sorting is very easy to implement, and the algorithm code is short and simple. In addition, the efficiency of the hill algorithm in the worst case is not much different from that in the average case, while the efficiency of fast sorting in the worst case is very poor. Experts advocate that almost any sorting work can be sorted by hill at the beginning. If it is proved that it is not fast enough in actual use,
Then, it is changed to a more advanced sorting algorithm such as quick sorting. In essence, an improvement of the hill sorting algorithm reduces the number of copies and the speed is much faster. The reason is that when the N value is large, the number of data items in each sort is very small, but the distance between data items is very long. When the N value is reduced by an hour, the amount of data to be exchanged increases, which is close to the final position after their sorting. It is the combination of these two cases that makes Hill sorting more efficient than insert sorting.


Ii. Sort buckets

Basic Ideas
Assume that the input is a real number that is evenly distributed across [0, 1) intervals generated by a random process. Divide the interval [0, 1) into n sub-intervals of the same size, or buckets, and then distribute N input numbers to each bucket. Because the input numbers are evenly distributed in [), there is usually no large number in a bucket. To get the result, sort the number of buckets and then list the elements in each bucket in order.

C language implementation

/* Bucketsort */void bucketsort (elementtype * r, int Len) {elementtype * buckets [10]; // pointer array int n = 1; // used to obtain the int index of each integer; // array subscript counting index int indexs [10]; // index int I, J for each bucket subscript counting; // allocate dynamic memory as the bucket for (I = 0; I <10; ++ I) buckets [I] = (elementtype *) malloc (sizeof (elementtype) * Len ); // zero index for counting; for (I = 0; I <10; ++ I) indexs [I] = 0; // array to bucket for (I = 0; I <Len; ++ I) {n = (INT) (R [I] * 10 ); // printf ("% d", n); buckets [N] [indexs [N] ++] = R [I];} // sort every bucket for (I = 0; I <10; ++ I) {quicksort (buckets [I], indexs [I]);} // bucket to array for (I = 0; I <10; ++ I) for (j = 0; j <indexs [I]; ++ J) R [index ++] = buckets [I] [J]; // release dynamic memory for (I = 0; I <10; ++ I) free (buckets [I]);}


Here, we use quick sorting to sort each bucket. The running result is as follows:

Algorithm Analysis
The average time complexity of Bucket sorting is linear O (N + C), where C = N * (logn-logm ). If the bucket quantity m is larger than the same N, the efficiency is higher, and the best time complexity is O (n ). Of course, the bucket sorting space complexity is O (n + M). If the input data is very large and the number of buckets is also very large, the space cost is undoubtedly expensive. In addition, the bucket sorting is stable.

Reference: Baidu encyclopedia

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.