Base sorting of basic sorting Series

Source: Internet
Author: User

Base sort


I. Base sorting is a non-Comparative integer Sorting Algorithm. The principle is to cut an integer into different digits by the number of digits and then compare them by the number of digits.

Its implementation principle: unify all the values to be compared (positive integers) into the same digit length, and add zero before the number of shorter digits. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.


II. Specific operations: the true implementation of this sorting is through the queue device, the principle of first-in-first-out, by putting a single digit, ten digits, hundreds of digits, and other hexadecimal formats, put it in different queues (also known as buckets), and then get a new sequence based on the principle of first-in-first-out, and retrieve a new sequence by performing operations such as re-entering the bucket through a hundred bits, the final sequence is the sorted sequence.


3. Base sorting is different from other sorting. Generally, the sorting we see is obtained through comparison. Quick sorting and merging are no exception, this sorting is efficient and stable for integers. For the base sorting algorithm that requires n nodes m times to store temporary elements, the algorithm complexity is O (n ). The time complexity of the sequential queue and chained queue base sorting algorithms is the same as that of O (2mn ).

Next we will use decimal:

500,342, 45,666,006,841,429,134, 78,264 is used as an example:

First time:

0 500

1 841

2 342

3


4 134 264
5 45

6 666 006
7


8 78

9 429

Expected result: 500 841 342 134 45 264 006 78 666

Second:

0 500 006
1

2 429
3 134
4 841 342 45
5

6 264 666
7 78
8

9

Expected result: 500 006 429 134 841 342 45 264 666 78

Third time:

0 006 45 78
1 134
2 264
3 342
4 429
5 500
6 666
7

8 841
9

Expected result: 006 45 78 134 264 342 429 500 666

The sorting result is obtained.


Sort segment code:

/*** Radix_sort () * SNode * S is used to receive the tub address * @ param int a [] indicates to accept the array to be sorted * @ param int length indicates the length of the array to be sorted * @ param int d indicates hexadecimal, here we assume that * @ param int m represents the maximum number of digits to be compared * @ return none */void Radix_sort (DataType a [], int length, int d, int m) {int power = 1, k; // used to calculate the number of bucket loading int count = 1; // define d queues as a dynamic array Queue * tub; tub = (Queue *) malloc (sizeof (Queue) * d); // initialize cout for each Queue <"_________________________________________" <
 
  
>>>> Nth "<
  
   

Insert code segment:

/*** Queue insertion, that is, the bucket data loading operation * @ param Queue * Q is used to receive the transmitted address * @ param int num indicates the number of buckets * return Queue **/Queue * QueueAppend (queue * Q, int num) {/*** here we need to apply for a space for rear, front and initialize ** // * Queue * Q; * to determine whether there is space to apply, and whether the application is successful * if (Q! = NULL) {* free (Q); * Q = NULL; *} * Q = (Queue *) malloc (sizeof (Queue); * if (Q! = NULL) {* cout <"Q-the space requested by the header node and the End Node is successful! "<
    
     
Rear = NULL; * Q-> front = NULL; * // *** determine whether R is empty and release space * and then press */SNode * p = NULL for the requested space. if (p! = NULL) {free (p); p = NULL;} p = (SNode *) malloc (sizeof (SNode); if (p! = NULL) {cout <"Space Application successful! "<
     
      
Data = num; // The number of digits must be put here, for example, one digit, ten digits, and hundreds of digits. p-> next = NULL; // a new node has been successfully created./*** Insert the new node to the end of the queue. **/if (Q-> rear! = NULL) {Q-> rear-> next = p; Q-> rear = p;} if (Q-> front = NULL) {Q-> rear = p; q-> front = p;} cout <"Bucket loaded successfully! "<
      
       
Code for recycling section:

/*** Delete the elements in the chain Queue * @ param Queue * q address used to receive the elements to be recycled * @ param DataType * d used to store the elements to be reclaimed * @ return int */int QueueDelete (Queue * q, dataType * d) {SNode * p;/*** determines whether data exists in the memory. if data exists, it will be output. No 0 **/if (q-> front = NULL) is returned) {cout <"At this time, no elements are listed in the queue! "<
        
         
Front-> data; p = q-> front; q-> front = q-> front-> next;} if (q-> front = NULL) {q-> rear = NULL;} free (p); // release node memory space return 1 ;}
        
All code:

/*** The principle of base sorting is to sort buckets by queue * @ author cainiao * @ version 2014.6.15 */# include
        
         
# Include
         
          
# Include
          
           
# Define MaxSize 100 using namespace std; typedef int DataType; // defines the Node used for chained queue typedef struct Node {DataType data; struct Node * next;} SNode; // define a struct to put the header and tail pointer of the Queue together with typedef struct {SNode * rear; SNode * front;} Queue; // initialize the header and tail nodes void QueueInitiate (Queue * q) {q-> rear = NULL; q-> front = NULL; cout <"initialization successful! "<
           
            
Rear = NULL; * Q-> front = NULL; * // *** determine whether R is empty and release space * and then press */SNode * p = NULL for the requested space. if (p! = NULL) {free (p); p = NULL;} p = (SNode *) malloc (sizeof (SNode); if (p! = NULL) {cout <"Space Application successful! "<
            
             
Data = num; // The number of digits must be put here, for example, one digit, ten digits, and hundreds of digits. p-> next = NULL; // a new node has been successfully created./*** Insert the new node to the end of the queue. **/if (Q-> rear! = NULL) {Q-> rear-> next = p; Q-> rear = p;} if (Q-> front = NULL) {Q-> rear = p; q-> front = p;} cout <"Bucket loaded successfully! "<
             
               Front = NULL) {cout <"At this time, no elements are listed in the queue! "<
              
                Front-> data; p = q-> front; q-> front = q-> front-> next;} if (q-> front = NULL) {q-> rear = NULL;} free (p); // release node memory space return 1 ;} /*** output function * @ param int a [] is used to receive arrays * @ param int n indicates the length of the array * @ return none */void out_put (int a [], int n) {cout <"_________________________________________________" <
               
                 >>> Nth "<
                
                 













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.