One-step write algorithm (base sorting)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Base sorting is another distinctive sorting method. How is it sorted? We can follow a set of numbers below to explain: 12, 104, 13, 7, 9

 

 

(1) sorting by single digit is 12, 13, 104, 7, 9

 

(2) sort by 10 digits 104, 7, 9, 12, and 13.

 

(3) Sort 7, 9, 12, 13, and 104 again by bits.

 

Note that if a digit has the same number, the sorting result should be determined based on the array in the previous round. For example, the values of 07 and 09 are both 0 at the very bits, however, in the last round of sorting, 09 was behind 07. In the same example, 12 and 13 were both 1 in the very place, but because 12 was at the top of 13 in the last round, therefore, in the very-bit sorting, 12 should also be placed at the top of 13.

 

So, in general, the 10-base sorting algorithm should be like this?

 

(1) determine the data size and arrange the data;

 

(2) Based on the result of 1, determine the size of the data in the very bit and arrange the data. If the remainder of the data at this position is the same, the order between the data is determined based on the order of the previous round;

 

(3) and so on, continue to judge the data in percentile and kilobytes until all data in a certain bit is 0.

 

After talking about this, write our code. I also hope you can give it a try.

 

A) Calculate the data in a certain bit.

 

 

Int pre_process_data (int array [], int length, int weight)

{

Int index;

Int value = 1;

 

For (index = 0; index <weight; index ++)

Value * = 10;

 

For (index = 0; index <length; index ++)

Array [index] = array [index] % value/(value/10 );

 

For (index = 0; index <length; index ++)

If (0! = Array [index])

Return 1;

 

Return 0;

}

Int pre_process_data (int array [], int length, int weight)

{

Int index;

Int value = 1;

 

For (index = 0; index <weight; index ++)

Value * = 10;

 

For (index = 0; index <length; index ++)

Array [index] = array [index] % value/(value/10 );

 

For (index = 0; index <length; index ++)

If (0! = Array [index])

Return 1;

 

Return 0;

} B) the data on a certain bit is 0 ~ 10 sorting

 

Void sort_for_basic_number (int array [], int length, int swap [])

{

Int index;

Int basic;

Int total = 0;

 

For (basic =-9; basic <10; basic ++ ){

For (index = 0; index <length; index ++ ){

If (-10! = Array [index] & basic = array [index]) {

Swap [total ++] = array [index];

Array [index] =-10;

}

}

}

 

Memmove (array, swap, sizeof (int) * length );

}

Void sort_for_basic_number (int array [], int length, int swap [])

{

Int index;

Int basic;

Int total = 0;

 

For (basic =-9; basic <10; basic ++ ){

For (index = 0; index <length; index ++ ){

If (-10! = Array [index] & basic = array [index]) {

Swap [total ++] = array [index];

Array [index] =-10;

}

}

}

 

Memmove (array, swap, sizeof (int) * length );

}

 

 

C) sort the actual data according to the sorting result in B.

 

Void sort_data_by_basic_number (int array [], int data [], int swap [], int length, int weight)

{

Int index;

Int outer;

Int inner;

Int value = 1;

 

For (index = 0; index <weight; index ++)

Value * = 10;

 

For (outer = 0; outer <length; outer ++ ){

For (inner = 0; inner <length; inner ++ ){

If (-10! = Array [inner] & data [outer] = (array [inner] % value/(value/10 ))){

Swap [outer] = array [inner];

Array [inner] =-10;

Break;

}

}

}

 

Memmove (array, swap, sizeof (int) * length );

Return;

}

Void sort_data_by_basic_number (int array [], int data [], int swap [], int length, int weight)

{

Int index;

Int outer;

Int inner;

Int value = 1;

 

For (index = 0; index <weight; index ++)

Value * = 10;

 

For (outer = 0; outer <length; outer ++ ){

For (inner = 0; inner <length; inner ++ ){

If (-10! = Array [inner] & data [outer] = (array [inner] % value/(value/10 ))){

Swap [outer] = array [inner];

Array [inner] =-10;

Break;

}

}

}

 

Memmove (array, swap, sizeof (int) * length );

Return;

}

 

D) combine a, B, and c to form a base sorting until the data on a certain bit is 0.

 

 

Void radix_sort (int array [], int length)

{

Int * pData;

Int weight = 1;

Int count;

Int * swap;

If (NULL = array | 0 = length)

Return;

 

PData = (int *) malloc (sizeof (int) * length );

Assert (NULL! = PData );

Memmove (pData, array, length * sizeof (int ));

 

Swap = (int *) malloc (sizeof (int) * length );

Assert (NULL! = Swap );

 

While (1 ){

Count = pre_process_data (pData, length, weight );

If (! Count)

Break;

 

Sort_for_basic_number (pData, length, swap );

Sort_data_by_basic_number (array, pData, swap, length, weight );

Memmove (pData, array, length * sizeof (int ));

Weight ++;

}

 

Free (pData );

Free (swap );

Return;

}

Void radix_sort (int array [], int length)

{

Int * pData;

Int weight = 1;

Int count;

Int * swap;

If (NULL = array | 0 = length)

Return;

 

PData = (int *) malloc (sizeof (int) * length );

Assert (NULL! = PData );

Memmove (pData, array, length * sizeof (int ));

 

Swap = (int *) malloc (sizeof (int) * length );

Assert (NULL! = Swap );

 

While (1 ){

Count = pre_process_data (pData, length, weight );

If (! Count)

Break;

 

Sort_for_basic_number (pData, length, swap );

Sort_data_by_basic_number (array, pData, swap, length, weight );

Memmove (pData, array, length * sizeof (int ));

Weight ++;

}

 

Free (pData );

Free (swap );

Return;

}

 

 

Summary:

 

(1) Pay attention to negative numbers during testing.

 

(2) If the data in a certain bit is the same, you need to consider the last round of data sorting.

 

(3) allocate small spaces multiple times in the Code. The Code to be optimized here

 

 

 

 

 

Supplement:

 

(1) The range of the remainder value was modified on the evening of January 1, October 15, so that negative numbers can also be sorted.

 

(2) A swap memory allocation was added on the morning of April 9, October 16 to avoid repeated memory allocation and release.

 

(3) The count is deleted on the morning of June 17, October 16. Once any data not equal to 0 is found, the return value is 1, and no data needs to be traversed.

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