Base sort and bucket sort C implementation

Source: Internet
Author: User
I. Algorithm descriptionBase sort (for example in shaping), split the shaping 10 into each bit, then compare each bit from low to high. Mainly divided into two processes: (1) allocation, starting from the single digit, according to the bit value (0-9) in the 0~9 bucket (such as 53, bits 3, then into the 3rd barrels) (2) collection, and then placed in the 0~9 bucket of data in order into the array repeating (1) (2) process, From single-digit to highest-bit (such as 32-bit unsigned shaping maximum number 4294967296, highest bit 10-bit) take "521 310 72 373, 15 546 385 856 187 147" sequence as an example, as shown in the following figure:

In the data, the highest level is 3, and after three allocations, the collection process becomes an ordered array. Two. Algorithm analysis

Average time complexity: O (DN) (d means the highest number of digits for shaping)

Space complexity: O (10n) (10 for 0~9, for storing temporary sequences)

Stability: Stablethree. Algorithm implementation [CPP] view plaincopy/********************************************************  * Function name:getnuminpos  * Parameter description:num  A shaping data   *          pos  Represents the POS bit data for the shaping to get   * Description:     Find num's data from low to high POS bits   *********************************** /   Int getnuminpos (int num,int pos)    {       int temp = 1;       for  (int i  = 0; i < pos - 1; i++)             temp *= 10;          return  (num /  temp)  % 10;  }     /*********************************************   * Function name:radixsort  * Parameter Description:pdataarray  unordered array;  *         &nbsp idatanum: Number of unordered data   * Description:     radix sort   ******************************************** /   #define &NBSP;RADIX_10&NBSP;10&NBSP;&NBSP;&NBSP;&NBSP;//sorting    #define   keynum_31    //number of keywords, here is the number of shaping digits    void radixsort (int* pdataarray, int  Idatanum)    {       int *radixArrays[RADIX_10];    The  //is the 0~9 sequence space        for  (int i = 0; i <  10; i++)        {            radixArrays[i] =  (int *) malloc (sizeof (int)  *  (idatanum + 1));            radixArrays[i][0] = 0;     Index 0 records the number of this set of data        }               for  (int pos = 1; pos <= keynum_31; pos++)      //from bits to 31        {            for  (int i = 0; i < idatanum; i++)      //distribution Process            {                int num = getnuminpos (PDataArray[i],  pos);               int index  = ++radixArrays[num][0];                radixArrays[num][index] = pDataArray[i];            }              for  (int i  = 0, j =0; i < radix_10; i++)     //Collection             {  

Related Article

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: 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.