算法:最大子数组own

时间:2024-08-01 13:35:26

转载标明出处:http://i.cnblogs.com/EditPosts.aspx?postid=4726782&update=1

暴力法:

 // maxValue.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream> using namespace std; int findMax(int *a,int low,int high,int &sub_left,int &sub_right,int &sub_sum)
{
if (low==high)
{
if(a[low]>)
{
sub_left=low;
sub_right=high;
sub_sum=a[low];
}
else sub_sum=;
} else
{
for(int i=;i<=high;i++)
{
int tempSum=;
for (int j=i;j<=high;j++)
{
tempSum+=a[j];
if (tempSum>sub_sum)
{
sub_sum=tempSum;
sub_left=i;
sub_right=j;
}
}
}
}
return ;
} int _tmain(int argc, _TCHAR* argv[])
{
int sub_left=;
int sub_right=;
int sub_sum=;
int a[]={,-,-,,-,-,-,,,-,,-,-,,-,};
findMax(a,,,sub_left,sub_right,sub_sum);
cout<<sub_left<<" "<<sub_right<<" "<<sub_sum<<endl;
system("pause");
return ;
}

时间复杂度:O(n*n);

分治法:

// maxSubArray.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream> using namespace std; int Find_Max_cross_subArray(int *a,int low,int middle,int high,int &max_left,int &max_right,int &max_value)
{
int left_sum=-;
int sum=;
for (int i=middle;i>=low;i--)
{
sum+=a[i];
if (sum>left_sum)
{
left_sum=sum;
max_left=i;
}
} int right_sum=-;
sum=;
for (int j=middle+;j<=high;j++)
{
sum+=a[j];
if (sum>right_sum)
{
right_sum=sum;
max_right=j;
}
}
max_value=left_sum+right_sum;
return ; } int Find_Max_subArray(int *a,int low,int high,int &max_left,int &max_right,int &max_vlaue)
{
if (low==high)
{
if (a[low]>)
{
max_vlaue=a[low];
max_left=low;
max_right=high;
}
else max_vlaue=;
}
else
{
int middle=(low+high)/;
int tem_left_low;
int tem_left_high;
int tem_left_sum;
Find_Max_subArray(a,low,middle,tem_left_low,tem_left_high,tem_left_sum); int tem_right_low;
int tem_right_high;
int tem_right_sum;
Find_Max_subArray(a,middle+,high,tem_right_low,tem_right_high,tem_right_sum); int tem_cross_low;
int tem_cross_high;
int tem_cross_sum;
Find_Max_cross_subArray(a,low,middle,high,tem_cross_low,tem_cross_high,tem_cross_sum); if (tem_left_sum>=tem_right_sum&&tem_left_sum>=tem_cross_sum)
{
max_left=tem_left_low;
max_right=tem_left_high;
max_vlaue=tem_left_sum;
}
else if (tem_right_sum>=tem_left_sum&&tem_right_sum>=tem_cross_sum)
{
max_left=tem_right_low;
max_right=tem_right_high;
max_vlaue=tem_right_sum;
}
else
{
max_left=tem_cross_low;
max_right=tem_cross_high;
max_vlaue=tem_cross_sum;
}
}
return ;
} int _tmain(int argc, _TCHAR* argv[])
{
int max_left=;
int max_right=;
int max_value=;
int a[]={,-,-,,-,-,-,,,-,,-,-,,-,};
Find_Max_subArray(a,,,max_left,max_right,max_value);
cout<<max_left<<" "<<max_right<<" "<<max_value<<endl;
system("pause");
return ;
}

递归式: T(n)=2*T(n/2)+Theta(n);n>1

T(n)=theta(1);

非递归线性算法:(4.1-5)

此方是看了我转载的那个伙伴的才有思路,和为负数,就跳过。

 // Max_Value.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
using namespace std; int Max(int *a,int low,int high,int &sub_left,int &sub_right,int &sub_sum)
{
if (low==high)
{
if (a[low]>)
{
sub_sum=a[low];
sub_left=low;
sub_right=high;
}
else sub_sum=;
}
int tempSum=;
for (int i=low;i<=high;i++)
{
tempSum+=a[i];
if (tempSum<=)
{
tempSum=;
sub_left=i+;
}
if (tempSum>sub_sum)
{
sub_sum=tempSum;
sub_right=i;
}
}
return ;
}
int _tmain(int argc, _TCHAR* argv[])
{
int sub_left=;
int sub_right=;
int sub_sum=;
int a[]={,-,-,,-,-,-,,,-,,-,-,,-,};
Max(a,,,sub_left,sub_right,sub_sum);
cout<<sub_left<<" "<<sub_right<<" "<<sub_sum<<endl;
system("pause");
return ;
}

只扫描了一遍,时间复杂度为O(n);

以上方法输出结果都一样:7 10 43