Counting sorting and radix ordering of common sorting algorithms

Source: Internet
Author: User

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/37/wKiom1e4daHCpR38AAEKCEW4XWs073.png "style=" float: none; "title=" 1.png "alt=" Wkiom1e4dahcpr38aaekcew4xws073.png "/>

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/86/37/wKiom1e4daGj_cMNAAENhyAGQkY362.jpg "style=" float: none; "title=" 2.jpg "alt=" Wkiom1e4dagj_cmnaaenhyagqky362.jpg "/>


1. Counting sort

As the name implies, the data in the sorted array is counted and then sorted according to the statistics, for example:

Array to sort:

A[] = {100, 123, 112, 123, 201, 123, 112, 156, 156, 178, 185, 156, 123, 112}

First take the maximum and minimum values in the array (this goes without saying)

Maximum value:201 min:

So the array has a value range of 201-100 + 1 = 102

Open an array of size 102 count[102] and initialize all elements to 0

Iterate over the array again to a[i-100] as the subscript for the array count, adding 1 to the item

The resulting count array should be (here the count element is 0 items will not appear, too many)

count[0] = 1

count[12] = 3

count[23] = 4

count[56] = 3

count[78] = 1

count[85] = 1

count[101] = 1

The rest of the elements are 0.

The count array is traversed, and each element corresponds to a write back to the A array, at which point the array a should be

A = {100,112,112,112,123,123,123,123,156,156,156,178,185,201}

As you can see, the array a is already sorted.

The code is as follows:

void countsort (int* a, size_t n)   //count Sort { ASSERT (a);int min = a[0];int max = a[0];for  (int i = 1;  i < n; ++i) {if  (A[i] > max) max = a[i];else if  (a [I] < min) min = a[i];} int countnum = max - min + 1;int* countarray = new int[ Countnum];memset (countarray, 0, sizeof (int) * (countnum));for  (int i = 0; i  < n; ++i) {++countarray[a[i] - min];} int index = 0;for  (Int i = 0; i < countnum; ++i)   //writeback array {while  (countarray[i]--) {a[index++] = i + min;}} Delete[] countarray;} 

As you can see, the counting sort has a lot of limitations, and it is suitable for sorting data collections in a relatively centralized range of data.

2. Base sorting

Let's look at an example of how the cardinality sort is sorted.

With an initial sequence of: R {0, 123, 543, 187,, 2, one, and

We know that in any Arab number, the cardinality of its various digits is expressed in 0~9.

So we might as well think of 0~9 as 10 barrels.

We classify the number of single digits of a sequence and divide it into a specified bucket. For example: r[0] = 50, single digit is 0, this number is stored in the bucket numbered 0.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/37/wKioL1e4d6uRsTkEAAAwOTvd7Qw469.png "title=" 3.png " alt= "Wkiol1e4d6urstkeaaawotvd7qw469.png"/>


After sorting, we take all the numbers out of each bucket, sequentially, in order from number 0 to number 9.

At this point, the resulting sequence is a sequence of incremental trends on a single digit.

Sort by single digit: {50, 30, 0, 100, 11, 2, 123, 543, 187, 49}

Then follow the 10-bit order ( Note: No 10-bit number is treated as 10-bit 0 )

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/86/37/wKiom1e4fEjwu8H9AAApjPX2jxY150.jpg "title=" 4.jpg " alt= "Wkiom1e4fejwu8h9aaapjpx2jxy150.jpg"/>

Sort by 10-digit number:  {0, 2, one, 123, 543, 187}

Then follow the order of the Hundred ( > Note: There are no hundred numbers according to the Hundred 0 processing )

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/86/37/wKiom1e4fPvhUfdiAAAwuzti62Y012.jpg "title=" 5.jpg " alt= "Wkiom1e4fpvhufdiaaawuzti62y012.jpg"/>

follow Number of hundred sorts:  {0, 2, one, a, 123, 187, 543}

Sort complete 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0058.gif "alt=" J_0058.gif "/>650" this.width=650; " Src= "Http://img.baidu.com/hi/jx2/j_0058.gif" style= "Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; Font-size:24px;line-height:24px;white-space:normal, "alt=" J_0058.gif "/>650" this.width=650; "src=" http:/ Img.baidu.com/hi/jx2/j_0058.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:24px ; line-height:24px;white-space:normal; "alt=" J_0058.gif "/>650) this.width=650; src=" http://img.baidu.com/hi/ Jx2/j_0058.gif "style=" Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:24px;line-height:24px ; white-space:normal; "alt=" J_0058.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0058.gif " Style= "Color:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; Font-size:24px;line-height:24px;white-space: Normal, "alt=" J_0058.gif "/>650" this.width=650; src= "Http://img.baidu.com/hi/jx2/j_0058.gif" style= "COlor:rgb (0,112,192); font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:24px;line-height:24px;white-space:normal; " alt= "J_0058.gif"/>

code as follows:

Int getmaxbit (int* a, size_t n)   //gets the most digits in the array {assert (a); int bitnum =  1;int tmpData = 10;for  (Int i = 0; i < n; ++i) {while  (Tmpdata <= a[i]) {++bitnum;tmpdata *= 10;}} Return bitnum;} Void radixsort (int* a, size_t n)   //radix sort {assert (a);int maxbit =  Getmaxbit (a, n); int digit = 1;int* tmp = new int[n];int countbit[ 10];  //count int startbit[10];  //start position for  (int i = 1; i  <= maxbit; ++i) {memset (countbit, 0, sizeof (int) * 10);for  (int i =  0; i < n; ++i) {int bit =  (a[i]/digit)%10;  //first people, in 10 hundred ... ++countbit[bit];} memset (startbit, 0, sizeof (int) * 10);startbit[0] = 0;for  (int i =  1; i&Nbsp;< 10; ++i) {startbit[i] = startbit[i - 1] + countbit[i - &NBSP;1];} for  (int i = 0; i < n; ++i)   //deposit to temporary array {Int index  = startbit[(A[i]/digit)%10]++;tmp[index] = a[i];} for  (int i = 0; i < n; ++i)   //write back to original array {a[i] = tmp [i];} digit *= 10;} Delete[] tmp;}


650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "alt=" J_0003.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" white-space:normal; "alt=" J_0003.gif "/>650" this.width=650; "src = "Http://img.baidu.com/hi/jx2/j_0003.gif" style= "white-space:normal;" alt= "J_0003.gif"/>650 "this.width=650;" Src= "Http://img.baidu.com/hi/jx2/j_0003.gif" style= "White-space:normal; alt=" J_0003.gif "/>650) this.width=650 , "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" White-space:normal; alt= "J_0003.gif"/>650) this.width= 650, "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" white-space:normal; "alt=" J_0003.gif "/>650" this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" White-space:normal; alt= "J_0003.gif"/> 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" White-space:normal "alt=" j_0003.gif "/ >650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" White-space:normal, "alt=" J_0003.gif "/>650) this.width=650; src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" White-space: "alt=" J_0003.gif "/>650" this.width=650; src= "Http://img.baidu.com/hi/jx2/j_0003.gif" alt= "j_0003.gif"/ >650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" White-space:normal "alt=" j_0003.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "style=" White-space:normal "alt=" j_0003. GIF "/>

This article is from the "11408774" blog, please be sure to keep this source http://11418774.blog.51cto.com/11408774/1840714

Counting sorting and radix ordering of common sorting algorithms

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.