查找整数数组中“和最大的子数组”的几种实现

#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <assert.h>
#define DEBUG_INFO 4
using namespace std;

int Mystrstr(char*, const char*);//find the matching string postion
char* ReverseString(char*);
struct subSet{
       int * array ;
       int offset ;
       int length ;  
       int maxValue ;     
       };
struct subSet * MaxValueSubSet1(int *, int , struct subSet *); //O(n**3)
struct subSet * MaxValueSubSet2(int *, int , struct subSet *); //O(n**2)
struct subSet * MaxValueSubSet3(int *, int ,int, struct subSet *); //O(n*logn)
struct subSet * MaxValueSubSet4(int *, int , struct subSet *); //O(n)
int *multi(int *, int *, int n);
int main(int argc, char *argv[])
{

int length = 15;
int array[15] = {1, -1, 8, -6, 6, 5 , 10, 4 ,7 , -8, 9 , -3, -5 , -4, 3};
struct subSet returnSubSet;   
returnSubSet = *MaxValueSubSet1(array, length, &returnSubSet);
cout<<"MaxValueSubset1\n"<<"array:" ;
for (int i = 0; i < length;cout<<array [i ++]<<",");
cout<<"\n" ;
cout<<"max Value:"<<returnSubSet.maxValue<<"\n";
cout<<"offset:"<<returnSubSet.offset<<"\n";
cout<<"length:"<<returnSubSet.length<<"\n";

//MaxValueSubset2
returnSubSet = *MaxValueSubSet2(array, length, &returnSubSet);
cout<<"MaxValueSubset2\n"<<"array:" ;
for (int i = 0; i < length;cout<<array [i ++]<<",");
cout<<"\n" ;
cout<<"max Value:"<<returnSubSet.maxValue<<"\n";
cout<<"offset:"<<returnSubSet.offset<<"\n";
cout<<"length:"<<returnSubSet.length<<"\n";

//MaxValueSubset4
returnSubSet = *MaxValueSubSet4(array, length, &returnSubSet);
cout<<"MaxValueSubset4\n"<<"array:" ;
for (int i = 0; i < length; cout<<array [i++]<<",");
cout<<"\n" ;
cout<<"max Value:"<<returnSubSet.maxValue<<"\n";
cout<<"offset:"<<returnSubSet.offset<<"\n";
cout<<"length:"<<returnSubSet.length<<"\n";

    
    system("PAUSE");
    return EXIT_SUCCESS;
}


} 

struct subSet * MaxValueSubSet1(int *array, int length, struct subSet *ReturnSubSet)
{
    //O(n**3)
    assert(array != NULL);
    assert(length >= 0);
    assert(ReturnSubSet != NULL);
    
    int i, j, k;
    i = 0;
    j = 0;
    k = 0;
    int maxValue = 0;
    int tempValue = 0;
    (*ReturnSubSet).array = array;
    
    for (i = 0; i < length; i ++)
    {
        for ( j = i; j < length; j ++)
        {
            tempValue = 0;
            for ( k = i; k <= j; k ++)
            {  
                tempValue += array[k];
            }
            if (tempValue > maxValue)
            {
                maxValue = tempValue;                
                (*ReturnSubSet).offset = i;
                (*ReturnSubSet).length = k - i ;         
                (*ReturnSubSet).maxValue = maxValue;          
            }
        }
        
    } 
    return ReturnSubSet; 
    
}   

struct subSet * MaxValueSubSet2(int *array, int length, struct subSet *ReturnSubSet)
{
    //O(n**2)
    assert(array != NULL);
    assert(length >= 0);
    assert(ReturnSubSet != NULL);
    
    int i, j, k;
    i = 0;
    j = 0;
    k = 0;
    int maxValue = 0;
    int tempValue = 0;
    (*ReturnSubSet).array = array;
    // initialize the array;
    int accumArray[ length ];
    accumArray [0] = array [0];
    for (i = 1; i < length; i ++)
    {
        accumArray [i] =  accumArray [ i - 1] + array [i];
    }
        
        
    for (i = 0; i < length; i ++)
    {
        for ( j = i; j < length; j ++)
        {
            if ( i == 0)
            {
                tempValue = accumArray[j] - 0; 
            }else
            {
                tempValue = accumArray[j] - accumArray [i -1]; 
            }            
            
            if (tempValue > maxValue)
            {
                maxValue = tempValue;                
                (*ReturnSubSet).offset = i;
                (*ReturnSubSet).length = j + 1 - i ;         
                (*ReturnSubSet).maxValue = maxValue;          
            }
        }
        
    } 
    return ReturnSubSet;     
}   

//devide and conqure. O(n*logn), not logicly correct.
struct subSet * MaxValueSubSet3(int *array, int offset, int length, struct subSet *ReturnSubSet)
{
    
    assert(array != NULL);
    assert(length >= 0 && offset >= 0 && offset < length);
    assert(ReturnSubSet != NULL);
    
    (*ReturnSubSet).array = array;
    if (length == 0)
    {
        (*ReturnSubSet).offset = offset;
        (*ReturnSubSet).length = 0;         
        (*ReturnSubSet).maxValue = 0; 
        return ReturnSubSet;        
    }
    if (length == 1)
    {
        (*ReturnSubSet).offset = offset;
        (*ReturnSubSet).length = array[offset] >= 0? 1 : 0;         
        (*ReturnSubSet).maxValue = max(0, array[offset]); 
        return ReturnSubSet;        
    }
    
    int i, j, k, splitOffset;
    i = 0;
    j = 0;
    k = 0;
    splitOffset = 0;
    int maxValue = 0;
    int lmaxValue = 0;
    int rmaxValue = 0;
    int tempValue = 0;
    int ltemOffset = 0;
    int rtemOffset = 0;
    
    struct subSet subSet1;
    struct subSet subSet2;
    
    splitOffset = offset + length/2; 
    
    for (i = splitOffset, tempValue = 0, lmaxValue = 0, ltemOffset = splitOffset; i >= offset; i --)
    {
        tempValue += array[i];
        if (tempValue > lmaxValue)
        {
            lmaxValue = tempValue;
            ltemOffset = i;          
        }
    }     
    
    for (i = splitOffset +1, tempValue = 0, rmaxValue = 0, rtemOffset = splitOffset +1 ; i <=  offset + length; i ++)
    {
        tempValue += array[i];
        if (tempValue > rmaxValue)
        {
            rmaxValue = tempValue;
            rtemOffset = i;
        }
    }   
 
    subSet1 = *MaxValueSubSet3(array, offset, splitOffset + 1 - offset, &subSet1); 
    subSet2 = *MaxValueSubSet3(array, splitOffset + 1 , length - (splitOffset + 1 - offset), &subSet2); 
    if (lmaxValue + rmaxValue >= subSet1.maxValue && lmaxValue + rmaxValue >= subSet2.maxValue )
    {
        (*ReturnSubSet).offset = ltemOffset;
        (*ReturnSubSet).length =  rtemOffset + 1 - ltemOffset;         
        (*ReturnSubSet).maxValue = lmaxValue + rmaxValue; 
                  
    }
    else if (lmaxValue + rmaxValue <= subSet1.maxValue && subSet2.maxValue <= subSet1.maxValue )
    {
        (*ReturnSubSet).offset = subSet1.offset;
        (*ReturnSubSet).length =  subSet1.length;         
        (*ReturnSubSet).maxValue = maxValue; 
    }
    else if (lmaxValue + rmaxValue <= subSet2.maxValue && subSet1.maxValue <= subSet2.maxValue )
    {
        (*ReturnSubSet).offset = subSet2.offset;
        (*ReturnSubSet).length =  subSet2.length;         
        (*ReturnSubSet).maxValue = maxValue;
    }
    else
    {
        cout<<"vital logic error happened!";    
    }   
    
    return ReturnSubSet;     
}   

//scanning
struct subSet * MaxValueSubSet4(int *array, int length, struct subSet *ReturnSubSet)
{
    //O(n**2)
    assert(array != NULL);
    assert(length >= 0);
    assert(ReturnSubSet != NULL);
    
    int i, j, k;
    i = 0;
    j = 0;
    k = 0;
    int tempValue = 0;
    
    (*ReturnSubSet).offset = 0;
    (*ReturnSubSet).length = 0;
    (*ReturnSubSet).maxValue = 0;
    
    for (i = 0; i < length; i ++)
    {
        tempValue += array [i];
        if (tempValue <0)
        {
            tempValue = 0;
            (*ReturnSubSet).offset = i +1;             
        }
        if (tempValue > (*ReturnSubSet).maxValue)
        {
             (*ReturnSubSet).maxValue = tempValue;  
             (*ReturnSubSet).length = i +1 -(*ReturnSubSet).offset;       
        }
    }   
  
    return ReturnSubSet;     
}   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值