Sorting supplement (count base sort)

Source: Internet
Author: User

1 voidCountsort (int*A, size_t size)2 {3     intmax = a[0], min = a[0];4      for(inti =1; i < size; ++i)5     {6         if(Max <A[i])7         {8Max =A[i];9         }Ten         if(Min >A[i]) One         { AMin =A[i]; -         } -     } the     intindex =0; -     int*countarray =New int[Max-min +1]; -memset (Countarray,0,sizeof(int) * (Max-min +1)); -      for(inti =0; i < size; ++i) +     { -Countarray[a[i]-min]++; +     } A      for(inti =0; I < Max-min +1; ++i) at     { -          for(intj =0; J < Countarray[i]; ++j) -         { -a[index++] = i +min; -         } -     } in}

The so-called Cardinal order principle and the hash table, suitable for use in the number to be sorted in a relatively small range, open up a certain auxiliary space, according to the direct addressing method, the corresponding position of the auxiliary space count increased, the last sort of as long as the previous built auxiliary array traversal output once

1 intGetmaxdigit (int*a,size_t size)2 {3     intdigit =1;4     intMax =Ten;5      for(inti =0; i < size; ++i)6     {7          while(A[i] >=max)8         {9digit++;TenMax *=Ten; One         } A     } -     returnDigit; - } the  - //How many arrays do you need? A count, a start and a collection of staging arrays? The last copy will be back!  - voidDigitsort (int*A, size_t size) - { +     intMaxdigit =Getmaxdigit (A, size); -     intCurdigit =1; +     intdigit =0; A     intcount[Ten]; at     intstart[Ten]; -     int*bucket =New int[size]; -      while(Digit <maxdigit) -     { -memset (Count,0,sizeof(int) *Ten); -memset (Start,0,sizeof(int) *Ten); in          for(inti =0; i < size; ++i) -         { to             intnum = a[i]/curdigit%Ten; +count[num]++; -         } thestart[0] =0; *          for(inti =1; I <Ten; ++i) $         {Panax NotoginsengStart[i] = start[i-1] + count[i-1]; -         } the          for(inti =0; i < size; ++i) +         { A             intnum = a[i]/curdigit%Ten; thebucket[start[num]++] =A[i]; +         } -memcpy (A, Bucket,sizeof(int)*size); $digit++; $Curdigit *=Ten; -     } -}

The radix sort is also called the bucket sort, here the code example is to complete a few numbers of the sort, can be seen first according to the size of the number of bits to sort (throw into the respective number of barrels (the bucket is of course ordered (0-9))) and then in order to collect, then according to 10 digits into the bucket, until the highest bit

I'm not using a similar list structure here, I'm using a sequential table

Keep it back, count the helper array (corresponding to 0-9 with several numbers), and use the start array to calculate the position of each number to be sorted in the array, which is equivalent to collecting

Sorting supplement (count base sort)

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.