Introduction to algorithms-sorting (4) counting sorting (linear time sorting)

Source: Internet
Author: User

Introduction to algorithms-sorting (4) counting sorting (linear time sorting)
Directory 1, Count sorting Introduction 2, flowchart 3, code implementation 4, performance analysis 5, reference content 1. What is counting sorting? Count sorting is a special sort algorithm. The sort algorithm previously introduced requires the logarithm to be compared, and the lower efficiency threshold is θ (nlgn ); the counting sorting can be sorted without comparing the logarithm. It is amazing that you only need to traverse the sorting array to sort it, and the efficiency is sort (n ).. Haha, that's amazing. Let's talk about it now !!!! Limit: Count sorting can only be performed on non-negative integers (0, 1, 2, 3... n) sorting: the basic idea of counting sorting is to determine the number of elements smaller than x for each input element x. With this information, you can place x directly in its position in the final output array. For example, if there are 10 people of different ages and eight people are younger than A, then the age of A is 9th. In this way, we can get the positions of other people, the order is sorted. 2. flowchart sorting algorithm steps: 1) count the number of times each element with the value of X appears in the array, and store it to Item X of array C. 2) number of accumulated element occurrences (the number of elements X including X cannot exceed) 3) Add the element X to the appropriate position to sort the pseudo code: CountSort (A, n, k) // A-array to be sorted; n-number of elements in the array to be sorted; k-an integer greater than or equal to the maximum value of elements in the array to be sorted; Step 1 flowchart 1) count the number of times each element with a value of X appears in the array, and store it to the X-Step 2 flowchart 2 of array C) step 3 flowchart 3) add element X to the proper position in sequence 3. Code Implementation (c ++) copy code 1 # include <iostream> 2 # include <vector> 3 # include <time. h> 4 using namespace std; 5 6 # define N 10 // sort array size 7 # Define K 100 // The sorting array range is 0 ~ K 8 9 void CountSort (vector <int> & A) 10 {11 // find the maximum and minimum values of the array to be sorted: 12 int min, max; 13 min = max = A [0]; 14 for (int I = 1; I <. size (); I ++) 15 {16 if (A [I] <min) min = A [I]; 17 else if (A [I]> max) max = A [I]; 18} 19 // define array B to store the exclusive number of 20 vectors <int> B (. size (); 21 // define array C, size (max-min + 1 ), C [I] is the number of 22 vectors where the value of A is I <int> C (max-min + 1); 23 for (int I = 0; I <max-min + 1; I ++) C [I] = 0; // initially 024 for (int I = 0; I <. size (); I ++) C [A [I]-min] ++; // count 25 for (in T I = 1; I <max-min + 1; I ++) C [I] + = C [I-1]; // accumulate 26 for (int I =. size ()-1; I> = 0; I --) 27 {28 B [C [A [I]-min]-1] = A [I]; // values of a are retrieved in reverse order and placed in A proper position in B, and reduced by 129 in C COUNT. // The array subscript starts from 0, therefore, minus 130 C [A [I]-min] --; 31} 32. assign (B. begin (), B. end (); // B assigned to A33} 34 // print the array 35 void print_element (vector <int> A) 36 {37 int len =. size (); 38 for (int I = 0; I <len; I ++) 39 {40 std: cout <A [I] <""; 41} 42 std: cout <std: endl; 43} 44 // random parameter sorting array, generating array range 0 ~ Integer 45 void Random (vector <int> & a, int n, int k) 46 {47 int I = 0; 48 srand (unsigned) time (NULL )); 49 while (I <n) 50 {51 a [I ++] = rand () % k; 52} 53} 54 55 int main () 56 {57 vector <int> vec_int (N); 58 Random (vec_int, N, K); 59 cout <"source array:"; 60 print_element (vec_int ); 61 CountSort (vec_int); 62 cout <"Sort array:"; 63 print_element (vec_int); 64 65 system ("PAUSE"); 66 return 0; 67} copy the Code 4. Perform Performance Analysis and Analysis on the Effect of fast sorting with good performance and the counting sorting described in this section Rate [the value of the sort array is 0 ~ 100 integer, array size respectively take 100, 0.153012, 0.2369821000 test] array size N fast sort (MS) Count sort (MS) 6.00759 4.6122810000 58.4422 16.0187100000 4176.58 169.768 analysis: when the number of array elements is large, the counting sorting efficiency is quite high! Note: When the number of elements in the sorted array is small, the efficiency will be reduced, and the memory consumption will increase when the array range is large. When to sort by Count: end: 1) the array to be sorted is a non-negative integer. 2) The and array has a small range and a large number of elements.

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.