Data Structure BASICS (15) and data structure basics 15

Source: Internet
Author: User

Data Structure BASICS (15) and data structure basics 15

Base sorting is an internal sorting algorithm that uses the "Multi-Keyword sorting" idea to implement "Single-Keyword sorting.

There are two methods to sort multiple keywords:

Ranking priority (LSD)

First, sort K [0] {the smallest digit} of the base, and divide the record sequence into several subsequences based on different values of K (0, sort K [1 ,..., K [D-1] and so on until the last keyword is sorted.

Highest priority (MSD)

First, K [D-1] {highest digit of the base} is sorted, then K [D-2] is sorted, and so on until K [0] is sorted.

 

Baidu encyclopedia introduces the base sorting as follows:

Radix sort is a distribution sort. It is also called the bucket sort method. As its name suggests, it uses some information about key values, elements to be sorted are allocated to certain "buckets" to achieve sorting. The base sorting method is a stable sorting. In some cases, the base sorting method is more efficient than other stability sorting methods.

 

Chain base sortingThe basic steps are as follows:

1. Store the records to be sorted in an array [or use a pointer to link them to form a linked list]

2. when "Allocation" is selected, records are allocated to different "linked list/chain queue" (that is, different buckets or heaps) based on the value of the current "keyword bit, the "keyword bit" recorded in each linked list is the same;

3. during "Collection", the value of the current keyword is increased from small to large (that is, the n linked lists (the size of n is the base size) are retrieved by numbers) extract the elements from each linked list and put them in the original array or linked list;

4. Repeat 2) and 3) Two Steps n times for each keyword bit.

 

For example, use LSD to sort the base number of {179,208,306, 93,859,984, 55, 9,271, 33} (a linked list or array:

[Step 1: sort by bit]

 

[Step 2: ten rows]


[Step 3: sort by hundred bits]

 

Code Implementation (take LSD as an example ):

// Search for the maximum number of digits in the array. template <typename Type> unsigned int maxBits (Type * begin, Type * end) {unsigned int bits = 1; // standard is used as the benchmark, if the element // in array is greater than standard, bits + 1 int standard = 10; for (Type * current = begin; current! = End; ++ current) {while (* current> = standard) {standard * = 10; ++ bits ;}} return bits ;}
/** Note: begin: array start end: array end radix: base */# define DEBUGtemplate <typename Type> void radixSort (Type * begin, Type * end, int radix) {// find the maximum number of digits in the array int bits = maxBits (begin, end); // if the base is radix, You Need To radix the linked list std :: list <Type> lists [radix]; // loop bits times for (int d = 0, factor = 1; d <bits; ++ d, factor * = 10) {// allocate... for (Type * current = begin; current! = End; ++ current) {// retrieve the number of corresponding locations (for example, the single digit is 1) int number = (* current)/factor) % 10; // you need to put it in the lists [number] In the (allocated to) linked list labeled as 1. push_back (* current);} // collect... type * current = begin; // collect the elements in the radix linked list for (int I = 0; I <radix; ++ I) {while (! Lists [I]. empty () {* current = lists [I]. front (); ++ current; lists [I]. pop_front () ;}# ifdef DEBUG // print the intermediate sorting result for (current = begin; current! = End; ++ current) {cout <* current <'';} cout <endl; # endif // DEBUG }}template <typename Type> void radixSort (Type * array, int arraySize, int radix) {return radixSort (array, array + arraySize, radix );}

Time Complexity Analysis:

Set the columns to be sorted as n records and d key codes. If the value range of the key codes is radix, the time complexity of the chain base sorting is O (d (n + radix )), among them, the allocation time complexity is O (n), and the collection time complexity is O (radix). A total of d Allocation and collection are performed.


Appendix-test code:

int main(){    int array[10];    for (int i = 0; i < 10; ++i)    {        array[i] = rand()%1000;    }    for (int i = 0; i < 10; ++i)    {        cout << array[i] << ' ';    }    cout << endl;    radixSort(array, 10, 10);    for (int i = 0; i < 10; ++i)    {        cout << array[i] << ' ';    }    cout << endl;    return 0;}

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.