Algorithm review-recursive and divide governance strategies

Source: Internet
Author: User

The basic idea of divide and conquer policy:

A n-scale problem is divided into k smaller subproblems. These subproblems are independent of each other and are the same as the original problems. Recursively solve these subproblems, and then combine the solutions of each subproblem to obtain the original solution.

You can describe it in the following mode:

Divide_and_conquer (p ){

If (| p | <= N0) adhoc (P );

Divide P into smaller subinstances P1, P2,..., PK;

For (I = 1; I <= K; I ++ ){

Yi = divide-and-conquer (PI );

}

Return Merge (Y1, Y2,... yk );

}

| P | indicates the scale of the problem P. (For details, see Wang Xiaodong's introduction to algorithm design and analysis or algorithms)

The following is a typical algorithm of the percentile policy:

1. the basic idea of mergesort is to divide the elements to be sorted into two child sets of roughly the same size and sort the two child sets respectively, finally, the sorted sub-sets are merged into the required sorted sets.

You can describe it in the following mode:

Void mergesort (type arr [], int L, int R ){
// Mergesort l... (L + r)/2, merge and sort l to (L + r)/2
Mergesort (ARR, L, (L + r)/2 );

// Mergesort (L + r)/2 + 1... r, sort (L + r)/2 + 1 to R by merging
Mergesort (ARR, (L + r)/2 + 1, R );

// Merger, merge the two sub-sequences in the sorted order to make the whole sequence orderly.

Merge (ARR, L, R );

}

The Code is as follows: insert the sorting directly for the last merge tasks I use.

Template <class T> void mergesort (T arr [], int L, int R) {If (L <r) {// mergesort l... (L + r)/2 mergesort (ARR, L, (L + r)/2); // mergesort (L + r)/2 + 1... rmergesort (ARR, (L + r)/2 + 1, R); // merger, direct insert sortfor (INT I = (L + r)/2 + 1; I <= r; I ++) {T temp = arr [I]; Int J = I-1; // The elements of the ordered sequence gradually increase, and the elements of the columns to be sorted gradually decrease. For (J; j> = 0; j --) {If (ARR [J]> temp) {arr [J + 1] = arr [J];} else {break ;}} arr [J + 1] = temp ;}}}

But in fact, using direct insert in the merge part may be an extremely bad thing. In the worst case, the time complexity of the merge operation (merge) is O (n ^ 2 ), for example, the unordered SEQUENCES {6, 7, 8, 5, 3, 1, 4, 2} form an ordered sequence {5, 6, 7, 8} and {1, 2, 3, 4} in the last merge. When performing the merge operation, because the algorithm is directly inserted, it must be the worst case. For a disordered sequence with n elements, the time complexity is O (n ^ 2) in the worst case ).

If you change the merge operation (merge) algorithm, the merge operation can be completed within O (N). You can certainly guess that it is the algorithm for merging two sorted linked lists.

===== March 26, 2013 update ================================================ ==========

Merge operation process: https://zh.wikipedia.org/wiki/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F#.E5.BD.92.E5.B9.B6.E6.93.8D.E4.BD.9C

  1. Apply for a space, so that the size is the sum of two sorted sequences. This space is used to store the merged sequence.
  2. Set two pointers. The initial position is the start position of the two sorted sequences respectively.
  3. Compare the elements pointed to by the two pointers, select a relatively small element into the merging space, and move the pointer to the next position.
  4. Repeat Step 3 until a pointer reaches the end of the sequence
  5. Copy all the remaining elements of another sequence directly to the end of the merged sequence.

========================================================== ================================

At this time, the calculation time t (n) required for the entire Merge Sorting is sufficient in the worst case.

T (n) = {O (1) n <= 1

{2 T (n/2) + O (n) n> 1

To solve this recursive equation, T (n) = O (nlogn) can be obtained. Because the lower bound of the computing time of the Sorting Problem Is nlogn, the Merge Sorting Algorithm is a progressive optimal algorithm.

2. Quick sorting

The idea of fast sorting:

Algorithm code:

Template <class T> void quicksort (T arr [], int L, int R) {If (L <r) {// a sort int T = L; t temp = arr [T]; int I = L, j = r; while (I <j) {for (; arr [J]> = temp & I <J; J --) {} If (ARR [J] <temp & I <j) {// exchange arr [T] = arr [J]; t = J; I ++;} For (; arr [I] <= temp & I <j; I ++) {} If (ARR [I]> temp & I <j) {arr [T] = arr [I]; t = I; j --;}} arr [I] = temp; t = I; // recursive call, decomposition problem quicksort (ARR, L, t-1); quicksort (ARR, t + 1, R );}}

Time Complexity: O (nlogn)

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.