C Language algorithm

Source: Internet
Author: User

You can use the divide and conquer method to solve the problem of sorting, which is to arrange n elements in a non descending order. The divide-and-conquer approach usually uses the following steps to sort the algorithm: if n is 1, the algorithm terminates; otherwise, the set of elements is divided into two or more child sets, each of which is sorted separately, and the ordered subset is merged into a single set.

Suppose that only the set of n elements is divided into two child sets. Now you need to determine how to partition the Child collection. One possibility is to put the n-1 element in the first subset (called a) and the last element in the second set (called B). Sort a recursively by this way. Since b contains only one element, it is sorted, and after a row, just use the function i n s e r T in program 2-1 0 to combine a and B. Comparing this sort algorithm with I n s e r t i o n s o r t (see program 2-1 5), we can find that this sort algorithm is actually a recursive algorithm for insertion ordering. The complexity of the algorithm is O (n 2). Another way to divide n elements into two sub sets is to place the elements with the largest value in B and the rest in a. Then a is sorted recursively. To merge sorted A and B, just add B to a. If you use the function m a x (see program 1-3 1) to find the maximum element, this sort algorithm is actually a recursive algorithm for S e l e c t i o n s o r t (see program 2-7).

If you use the bubbling process (see program 2-8) to find the largest element and move it to the rightmost position, this sort algorithm is the recursive algorithm for B u b l e S o r t (see program 2-9). The complexity of the two recursive sorting algorithms is (N2). The complexity of the algorithm is O (n2) (see examples 2-1 6 and 2-1 7) if it is found to terminate the recursive division of a If a has been ordered.

The above partitioning scheme divides n elements into two very unbalanced sets a and B. A has n-1 elements, and B contains only one element. Here's a look at what happens with the balanced split method: A set contains n/k elements, and B contains the rest of the elements. Recursively use the divide-and-conquer method to sort a and B. Then a process called merge (M e rg e) is used to combine the ordered A and b into a single set.

Example 2-5 considers 8 elements, and the values are [1 0,4,6,3,8,2,5,7] respectively. If k = 2 is selected, then [1 0, 4, 6, 3] and [8, 2, 5, 7] are individually sorted separately. The results were [3, 4, 6, 1 0] and [2, 5, 7, 8]. The two sorted sequences are merged from the head of the two sequences. The element 2:3 is smaller, is moved to the result sequence, 3 is compared with 5, 3 is moved into the result sequence, 4 is compared to 5, 4 is put into the result sequence; 5 and 6 Compare, ... If you select K= 4, then the sequence [1 0, 4] and [6, 3, 8, 2, 5, 7] will be sorted. The results of the sorting were [4, 1 0] and [2, 3, 5, 6, 7, 8]. When these two sorted sequences are merged, the desired sequence is obtained.

Figure 2-6 shows the pseudo code for the divide and conquer sorting algorithm. The number of neutron sets in the algorithm contains n/k elements in the 2,a.

template
void sort( T E, int n)
{ / /对E中的n个元素进行排序,k为全局变量
if (n >= k) {
i = n/k;
j = n-i;
令A 包含E中的前i个元素
令B 包含E中余下的j个元素
s o r t ( A , i ) ;
s o r t ( B , j ) ;
m e rge(A,B,E,i,j,); //把A 和B 合并到E
}
else 使用插入排序算法对E 进行排序
}

Fig. 14-6 pseudo code of the divide and conquer sorting algorithm

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.