Merge sort, maximum sub-array

Source: Internet
Author: User

1. Merge sort

Divide and Conquer mode:

(1) The decomposition of the original problem is a number of sub-problems, these sub-problems are small instances of the original problem.

(2) solving sub-problems, solving sub-problems recursively. The sub-problem is small enough to be solved directly.

(3) The solution of the sub-problem is solved by the original problem.

Merge sort completely follows the divide-and-conquer mode.

(1) The decomposition of n elements to be sorted is listed as two sub-sequences with N/2 elements.

(2) Sort two sub-sequences recursively by using merge sort.

(3) Merge two sorted sub-sequences to produce sorted answers.

Time complexity: T (n) =o (N*LGN)

Code:

#include <iostream>#include<vector>using namespaceStd;vector<int> Merge (vector<int> VEC1, vector<int>vec2) {Vector<int>VEC; Auto It1=Vec1.begin (); Auto It2=Vec2.begin ();  while(It1! = Vec1.end () && it2! =Vec2.end ()) {        if(*it1<*it2) {Vec.push_back (*it1); ++it1; }        Else{vec.push_back (*it2); ++it2; }    }     while(It1! =Vec1.end ()) {Vec.push_back (*it1); ++it1; }     while(It2! =Vec2.end ()) {Vec.push_back (*it2); ++it2; }    returnVec;}voidMerge_sort (vector<int> &VEC) {    if(Vec.size () >1)//{vector<int>VEC1;  for(Auto it = Vec.begin (); it!=vec.begin () + (Vec.end ()-Vec.begin ())/2; ++it) {Vec1.push_back (*it); } merge_sort (VEC1);//Vector<int>vec2;  for(Auto it = Vec.begin () + (Vec.end ()-Vec.begin ())/2; It! = Vec.end (); ++it) {Vec2.push_back (*it); } merge_sort (VEC2);//Vector<int> temp = Merge (VEC1, VEC2);//VEC= temp;////Save a sorted part of an element    }}intmain () {vector<int> VEC = {2,3,2,5,6,1, -1,3, -, A };    Merge_sort (VEC);  for(auto C:vec) cout<< C <<","; cout<<Endl; System ("Pause"); return 0;}

2. Maximum sub-array problem

In an array that contains negative numbers (if the array elements are all positive, the largest subarray is the entire array), find the sum of the largest sub-arrays of the elements.

Solution method using divide-and-conquer strategy:

With a[low...high],mid= (Low+high)/2, the largest sub-array A[I...J]:

(1) completely in A[low...mid], low<=i<=j<=mid.//sub-problem

(2) completely in A[mid+1...high], mid<i<=j<=high.//sub-problem

(3) Crossing the midpoint, low<=i<=mid<j<=high.//new problems

In three cases the selection and the largest person.

Pseudo code:

array<int,3> Find_max_subarray (Ivec,intLowintHigh ) {    if(low==High )return{Low,high,ivec[low]};//base Case:only One element    Else{        intMid= (Low+high)/2; Left=Find_max_subarray (Ivec,low,mid); Right=find_max_subarray (ivec,mid+1, high); Cross=Find_max_crossing_subarray (Ivec,low,mid, high); if(left[2]>=right[2]&&left[2]>=cross[2])            returnLeft ; Else if(right[2]>=left[2]&&right[2]>=cross[2])            returnRight ; Else            returnCross ;} Array<int,3> Find_max_crossing_subarray (Ivec,intLowintMidintHigh ) {    intleft_sum=MIN; intright_sum=MIN; intsum=0; intmax_left=mid,max_right=mid;  for(inti=mid;i>=low;i--) {sum+=Ivec[i]; if(sum>left_sum) {Left_sum=sum; Max_left=i; }} sum=0;  for(intJ=mid+1; j<=high;j++) {sum+=Ivec[j]; if(sum>right_sum) {Right_sum=sum; Max_right=J; }    }        return{max_left,max_right,left_sum+right_sum};//The leftmost subscript , the right subscript, the sum of the elements}

Time complexity: T (n) =o (N*LGN)

It is not difficult to find that if the array element is all negative, the largest sub-array is the largest element. Correspondingly, there are also minimal sub-array problems.

Merge sort, maximum sub-array

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.