Introduction to algorithms CLRS algorithm C ++ (6) P100 base sorting

Source: Internet
Author: User

Chapter 8 linear time sorting

8.3 base sorting

In the introduction to algorithms, there are only two rows to describe the algorithm for base sorting... Let's just say something more...

Radix-sort (a, d)

1 for i ← 1 to d2     do use a stable sort to sort array A on digit i

C ++ code

1 # include <iostream> 2 3 using namespace STD; 4 5 // calculate the maximum number of digits in the array. 6 int maxdigit (int * arr, int digit) 7 {8 int n = 0; 9 int * temp = new int [digit]; 10 for (INT I = 0; I <digit; I ++) 11 temp [I] = arr [I]; 12 for (INT I = 0; I <digit; I ++) 13 {14 int counter = 1; 15 while (temp [I]/10> 0) 16 {17 counter ++; 18 temp [I]/= 10; 19} 20 if (n <counter) 21 N = counter; 22} 23 Delete [] temp; 24 return N; 25} 26 27 // base sorting 28 void radixsort (int * arr, int digit) 29 {30 int n = maxdigit (ARR, digit); 31 int * temp = new int [digit]; 32 int * COUNT = new int [10]; 33 int I, j, k; 34 int Radix = 1; 35 for (I = 1; I <= N; I ++) 36 {37 for (j = 0; j <10; j ++) 38 count [J] = 0; 39 for (j = 0; j <digit; j ++) 40 {41 K = (ARR [J]/Radix) % 10; 42 count [k] ++; 43} 44 for (j = 1; j <10; j ++) 45 count [J] = count [J-1] + Count [J]; 46 for (j = digit-1; j> = 0; j --) 47 {48 k = (ARR [J]/Radix) % 10; 49 count [k] --; 50 temp [count [k] = arr [J]; 51} 52 for (j = 0; j <digit; j ++) 53 arr [J] = temp [J]; 54 Radix * = 10; 55} 56 Delete [] temp; 57 Delete [] count; 58} 59 60 int main () 61 {62 int A [] = {22, 34, 95, 87, 56,980, 12, 48}; 63 radixsort (A, 8); 64 for (INT I = 0; I <8; I ++) 65 {66 cout <A [I] <"; 67} 68 cout <Endl; 69 return 0; 70}

In the C ++ implementation, I used an auxiliary function int maxdigit (int * arr, int digit) to calculate the maximum number of digits in the array to be sorted, in order to make the program have a larger scope of application, do not need to be sorted with the same number of digits.

The base sorting algorithm is intuitive. In an array, each element has a D-digit number, of which 1st is the nth bit, and D is the highest bit. Judge the order of each position in sequence. Base sorting requires the assistance of another stable Sorting Algorithm (stable sort.

 

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.