Eighth chapter linear Time sequencing

Source: Internet
Author: User

Summary:

This chapter first reviews the features of merge sorting, heap sorting and quick sorting, and the running time of the previous introduction. Combined sorting and heap ordering are at worst an O (NLGN), while a fast sort is the worst case for O (n^2), with an average of O (NLGN), so the merge sort and heap ordering are asymptotically optimal. The order of the elements in the execution process is based on the comparison between the input elements, which is called the comparison sort. Next, the paper introduces the concept of decision tree and how to use decision tree to determine the lower bound of the scheduling algorithm, and finally discusses three kinds of linear time-running algorithms: counting sorting, cardinal sorting and bucket ordering. These algorithms are stable in that they do not need to compare elements to determine the order of the ordering during execution.

1. Decision Tree Model

In the comparison sorting algorithm, a comparison operation is used to determine the order of elements between the input sequence <A1,A2,......, a3>. The decision tree is a completely binary tree, and the comparison sort can be considered as a decision tree by the abstract, representing a comparison of the given input of a sort algorithm scope. In the decision tree, nodes are represented as I:J, where 1≤i,j≤n,n is the number of elements to be sorted, and leaf nodes are the result of sorting. The left subtree of the node satisfies the Ai≤aj, and the right subtree satisfies the Ai>aj. The necessary conditions for the correct operation of the sorting algorithm are: n Elements of the n! Each of the permutations in the arrangement appears as a leaf in the decision tree. For example, there is a sequence of a<3,2,1>, which has a small arrival to make an insertion sort, as shown in the decision tree of the order:

In the decision tree, the length of the longest path between any of the leaf nodes can be reached, indicating the worst-case comparison in the corresponding sorting algorithm.

Theorem: For a comparison sorting algorithm in the worst case, it is necessary to do the Ω (NLGN) this comparison.

Inference: Both heap sorting and merge ordering are the asymptotic optimal comparison sorting algorithms.

2. Sort by Count

The count sort assumes that each of the N input elements is an integer between 0 and K, and K is the largest element in the N number. When K=o (n), the run time of the count sort is θ (n). The basic idea of counting sorting is to count the number of elements that are less than or equal to x for each element x in n input elements, and to determine the final position of x in the output array based on the number of X. This process requires the introduction of two secondary storage spaces, the B[1...N of the results], to determine the array of the number of each element C[0...K]. The specific steps of the algorithm are as follows:

(1) Determine the value of K based on the value of the element in the input array A and initialize c[1....k]= 0;

(2) Iterate through the elements in the input array A, determine the number of occurrences of each element, and store the number of occurrences of the first element in a c[a[i]], and then c[i]=c[i]+c[i-1], in C to determine that each element in a is preceded by multiple elements;

(3) Reverse-iterate through the elements in array A, find the number of occurrences in a in C, and determine its position in the result array B, and then reduce its corresponding number of times in C by 1.

Give an example of the process, assuming that the input array a=<2,5,3,0,2,3,0,3>, the count sort process is as follows:

The pseudo-code for count ordering is given in the number:

1 Counting_sort (A,B,K) 2 for     i=0 to K 3 does       c[i] = 0 4 for j=1 to     length (A) 5 do         c[a[j]] = c[a[j]]+1   // C[i] contains the number equal to element I 6 for     I=1 to K 7 does         c[i] = C[i] + c[i-1]  //c[i] contains the number of less than equals element I 8 for     J=length[a] Downto 1 9< C9/>do B[c[a[j]] [= a[j]10              c[a[j]] = C[a[j]]-1

Question: In the Counting_sort process, the 8th line for loop is why for J=length[a] Downto 1, not for J=1 to Length[a].

Answer: The algorithm can still work normally, although the algorithm is changed from J=1 to Length[a], but it is not guaranteed to be stable at this time. Because if there are two elements that are the same, then the preceding one appears after the sort, followed by the preceding one, that is, the elements of the same value in the output array are in a different order than they are in the input array. And from the for J=length[a] Downto 1 can be guaranteed to be a stable algorithm.

The C + + code is as follows:

#include <iostream>using namespace std;void countingsort (int a[],int b[],int n,int k) {    int c[k];    int i;    for (i=0;i<k;i++)        c[i]=0;    for (i=0;i<n;i++)        c[a[i]]++;    for (i=1;i<k;i++)        c[i]+=c[i-1];    for (i=n-1;i>=0;i--)    {        b[c[a[i]]-1]=a[i];        c[a[i]]--;}    } int main () {    int arr[20]={1,2,2,4,5,6,3,2,5,3,4,7,8,6,8,5,0,2,8,9};    int b[20];    Countingsort (arr,b,20,10);    for (auto a:b)        cout<<a<< "";    Cout<<endl;}

From the thought and process of counting, we can see that when the number of input array A is large, it is not suitable. Because you need to open up the maximum number of auxiliary arrays, count the occurrences of each element. Usually the count sort is used in the cardinal sort, as a subroutine. The most important property of a count sort is that it is stable: elements with the same value are in the same order in the output array as they do in the input array.


3 Cardinality sorting

The Cardinal sort sort process does not need to compare keywords, but rather through the "assign" and "collect" procedures to achieve the order, its time complexity can reach the linear Order: O (n). For decimal numbers, each bit is in [0,9], and the number of D bits is column D. The cardinality sort is sorted first by the low-effective number, and then to the next-to-last order until the highest bit sort ends. Illustrate the cardinal sort process as shown in:

The cardinality sorting algorithm is straightforward, assuming that each element has a D-bit number in array a of length n, where the 1th bit is the lowest and the D-bit is the highest bit. The pseudo-code is shown in the book as follows:

1 Radix_sort (A,D) 2 for    I=1 to D3 does          usage A stable sort to SORT array A on digit I

Lemma: given n-d digits, each digit can take K-possible values. If the stable sort used requires a time of θ (n+k), the cardinality sorting algorithm performs the correct sorting of these numbers in θ (d (n+k)) time.

4, bucket sorting

The count sort assumes that the input is made up of integers in a small range, while the bucket ordering assumes that the input is produced by a random process that distributes the elements evenly and independently across the interval [0,1]. When the input of the bucket sort conforms to the uniform distribution, it can be expected to run linearly. The idea of barrel sequencing is: Divide the interval [0,1] into n equal-sized sub-ranges, become buckets (buckets), then distribute n input numbers into buckets, sort the number of buckets, and then list the elements in each bucket in order.

The pseudo-code for the bucket ordering is given, assuming that the input is an array of n elements, and each element satisfies 0≤a[i]<1, and an auxiliary array b[0....n-1] is required to hold the linked list (bucket). The pseudo-code looks like this:

1 Bucket_sort (a) 2    n = Length (a) 3 for    i= 1 to N4 does        insert a[i] into list B5 for    i=0 to n-16 do        SORT Li St B[i] with insertion sort7    concatenate the list b[0], b[1],,,b[n-1] together in order

To illustrate the process of sorting buckets, suppose there are now a={0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68}, the buckets are sorted as follows:

Eighth chapter linear Time sequencing

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.