Find the maximum subarray of an array (c + + implementation)

Source: Internet
Author: User

Maximum Subarray: required to connect, plus the largest sub-array is the largest subarray of an array. Compilation environment: VS2012, by the way, I am a C # programmer, I just like to learn C + +.

In fact, this is a semi-finished, there are some bugs in the inside, but the overall idea is this, the largest sub-array, from a median, is the middle position of the array, and then the middle position across, the left, and the right of the largest, and then compare the size of the three, the largest is the largest sub-array. Ideas come from the introduction of algorithms. Today is the implementation of pseudo-code, but seemingly a bit of a bug, do not know the great God to help me mention it? In order to save time, I simply put some of the explanation of the problem, is the introduction of the algorithm, I also read this book to do.

I declare that this is my own code, the following, I did not read other people write, mainly want to exercise their own, combined with the above algorithm introduction look basically there is no problem, good night.

ConsoleApplication8.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <iostream>using namespace std; Template <class t>int Getarraylen (t& array)//Use templates to define a function Getarraylen, which returns the length of an array of {return (sizeof (array) /sizeof (array[0]);} int _tmain (int argc, _tchar* argv[]) {void Findmaxsubarray (int [],int); int a[]={5,-100,34535,32,10,5,4,-100,2};// Initializes an array of int len=getarraylen (a); Findmaxsubarray (A,len); System ("pause"); return 0;} void Findmaxsubarray (int a[],int len) {//declares int getsubarrayspreiterator (int [],int, int,int); int getsubarraymid (int, int , int, int [],int);//low subscript int low=0;//high subscript int high=0;//median int mid=0;//maximum subarray int sum=0; The first case of int sumafter=0; The second case is int summid=0; The third case is int mid_rightpos=0; The right boundary int mid_leftpos=0 across the middle array; The left boundary int summidfromright=0 across the middle array; Computes the int arrlen=len;//array length from the right side//If the array has only one element if (arrlen==1) {cout<< "Maximum sub-array is:" &LT;&LT;A[0]&LT;&LT;ENDL;} MID=ARRLEN/2; Figure out the middle position/* There are 3 possible cases 1. The maximum subarray is on the left side of the median number 2. The maximum subarray is on the right side of the median 3The largest sub-array crosses the middle *///first for a case (int i=mid;i>=0;i--) {sum=sum+a[i];int sum_=getsubarrayspreiterator (a,mid,i,0); if (sum &LT;SUM_) {sum=getsubarrayspreiterator (a,mid,i,0); low=i;//maximum sub-array to the left of the boundary. }}cout<< "Maximum sub-array (left) is:" <<sum<<endl;cout<< "Low:" <<low<<endl;//the second case for (int i= mid+1;i<arrlen;i++) {sumafter=sumafter+a[i];int sum_after=getsubarrayspreiterator (a,mid,i,1); if (sumAfter< Sum_after) {sumafter=getsubarrayspreiterator (a,mid,i,1); high=i;} If the above if statement is not entered, it is the last if (high==0&&i==arrlen-1) {high=i;}} cout<< "Maximum sub-array (right) is:" <<sumAfter<<endl;cout<< "High:" <

  

Find the maximum subarray of an array (c + + implementation)

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.