Bucket Sorting and cardinality sorting

Source: Internet
Author: User

Before summarizing the common sorting algorithms based on the comparison model, the fastest of them also consumes O (NLOGN) time. But we should know that it is still possible to sort by linear time under certain conditions. Bucket sorting and cardinality sorting are the algorithms executed in linear time under suitable conditions.

Bucket sort (bucket sort):

Thought: If we limit the range of integers that need to be sorted, say we have n integers, ranging from 0 to m-1, we can use this information to get a quick sorting algorithm. We are detaining an array called T, size M, and initialized to 0. So T has m units (buckets), and they are all empty at the beginning. When I is read T[i] plus one. After all the inputs have been read, scan the array t, and print out the sorted table to the original array.

voidBucket_sort (intA[],intNintm) {    int*t, I, j =0; T= (int*)malloc(M *sizeof(int));  for(i =0; I < m; i++) T[i]=0;  for(i =0; I < n; i++) T[a[i]]++;  for(i =0; I < m; i++)         while(t[i]-->0) A[j++] =i;  Free(t);}

Initializing the T array takes time θ (m), scanning the array a takes time θ (n), and then scanning the array t time-consuming θ (m), other operations θ (1), plus θ (m+n), if m=θ (n), the bucket is sorted as θ (n).

Base sort (radix sort):

Cardinality sorting is a generalization of bucket sequencing, which is also known as a card sort, because it has been used to sort old-fashioned punch machines until modern computers appear before.

Thought: If the range of numbers is too large, it is unrealistic to use buckets, because there are too many buckets to be ordered. People's strategy is to use multi-barrel sequencing. A correct algorithm is to use the lowest bit-first method for bucket sequencing. Unlike bucket sequencing, where there may be multiple cases of grumble in a bucket, we can no longer use one-dimensional arrays. If you use a two-dimensional array, the size of each array must be n, and the total space requirement will be θ (n^2). We can consider a list of ways to achieve the cardinality of the sort. Start by creating 10 headers, recording 0,1,2,3...9 separately, and establishing links from 0 to 1 to 2 to 9. First order the element is added to the corresponding header by the number on the single digit. The second order scans 10 headers from 0 to 9, examining the 10 bits of the element that the header is connected to, and linking the element to the linked list of the corresponding header. The third time to examine the hundred ... Go down until all the elements are inserted into the 0 header, traverse the 0 header, and then output to the original array to finish sorting.

... Say so, suddenly made lazy, want to take a bath to sleep now, tomorrow and then put the code bar.

Bucket Sorting and cardinality sorting

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.