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;}


B) The data on a certain point 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);}

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;}

D) combine A, B, and C to form a base sorting until the data in a certain bit is0

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.

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.