Bucket Sorting and counting sorting

Source: Internet
Author: User

Suddenly want to write a bucket of their own sort, and then do after the class and found the count of sorting, I think it is very interesting. But the book did not give the code, so I wrote a code, super Rotten 0 0 The following is a brief introduction of these two sorts

Bucket sort

The sorting of buckets is the sorting of data according to the idea of hashing. Suppose there are m buckets, using the simplest hash (key) =key, so that no comparisons can be made to deposit the number into the corresponding bucket. To solve the problem of conflict, the way to find the chain is no longer applicable, using the independent chain method, each bucket as a linked list of the same elements stored in the form of the same element to define the partial order, this can achieve the stability of the order. Bucket sequencing takes a simple hashing strategy and is relatively easy to understand. At the same time, completely abandon the CBA-style way, thus can break the complexity O (NLOGN) boundary. The time complexity of O (n) can be achieved by the strategy of space-change time, and the cost is the extra space of O (m+n). Here is the bucket sort for an integer array, the code sucks. I haven't changed it, I've tested it:

1 structnode2 {3Nodeintv =0, node* s =NULL): Value (v), SUCC (s) {}4     intvalue;5node*succ;6 };7 voidInsert (node &a,intb//b insert behind a8 {9node* T =Newnode (b);Ten     if(A.SUCC) T-&GT;SUCC =A.SUCC; OneA.SUCC =T; A } -node* Bucketsort (intAintNintK//The range of numbers is [0,k] - { thenode* bucket =NewNode[k] (); -      for(inti =0; I < n; i++) - Insert (Bucket[a[i]],a[i]); -     returnbuckets; + } - voidShow (Node A) + { A      while(A.SUCC) at     { -A = * (A.SUCC);//The first node does not output as a sentinel -cout << A.value <<" "; -     } - } - intMain () in { -     inta[ the] = {2,0,1,3,3,0,1,9,7,7, the, One, -, A,Ten}; tonode* p = bucketsort (A, the, -); +      for(inti =0; I < -; i++) - Show (P[i]); theSystem"Pause"); *     return 0; $}

Here are the test examples, have been tested, but here only the results of the order into a list of application space and then output, no release of the operation, also did not deposit the original array or another array 0 0 see the specific requirements can be changed

Count sort

The basic strategy for counting sorting, based on the fact that an ordered sequence, the rank of element m, should be equal to the number of elements in the sequence that are less than or equal to M. So the counting sorting algorithm can be summed up as follows: Traversing the sequence, for each element, and then traversing the entire sequence, with an extra array to count. It is not difficult to see that the time complexity of O (n^2). Obviously, this complexity cannot be accepted.

Consider hashing methods to reduce complexity. Similarly, for [0,k] elements, select m buckets, assuming that the number of elements is N, first traverse the sequence and count with the bucket array. Subsequently, the count of each subsequent bucket = The quantity of the front bucket + its own count. Thus, the number 1 per bucket equals the rank of the corresponding element of the bucket. At the same time, the problem of the same element can also be handled, because when the element has other factors in the partial order, the number is also added to the count of the bucket array, the bucket array only need to output from the back forward, you can maintain this partial order. The implementation code is as follows:

1 /*Count Sort*/2 int* Countsort (intAintKintN//[0,k] The number of n in the range3 {4     int* TMP =New int[K];5     int* s =New int[n];6memset (TMP,0,sizeof(int) *k);7      for(inti =0; I < n; i++)//the count in the original array8tmp[a[i]]++;9      for(inti =0; I < K-1; i++)//Number of numbers that are less than the numberTenTmp[i +1] +=Tmp[i]; One      for(inti = n-1; I >=0; i--)//Reverse Output AS[--tmp[a[i]] = a[i];//The count hash array-1 is the rank that should be corresponding, and the number of the original array is assigned - delete[] tmp; -     returns; the}

The code has been tested. You can see that only two traversal of the original array, a trip to the bucket array can be completed, the time complexity of O (m+n). Similarly, the secondary space is an array of buckets and the spatial complexity is O (M). It is not difficult to see that the most suitable case for counting sorting is when M is far less than N, that is, the data is larger and the range is small. At this point, the time complexity is only O (n).

Bucket Sorting and counting 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.