Looking for the largest sub-array
Find_max_sub_array.h#include <stdint.h>int Find_max_crossing_subarray (int* A, int low, int mid, int. High, Int&am P Max_left, int& max_right, int& max_value) {int left_sum = 0xFFFFFFFF;//impossible minimum value int sum = 0; for (int i = Mid, i > low; i--) {sum = sum + a[i]; if (Sum > left_sum) {left_sum = sum; Max_left = i; }} int right_sum = 0xFFFFFFFF; The minimum value of sum = 0 is not possible; for (int j = mid + 1, J < High; J + +) {sum = sum + a[j]; if (Sum > right_sum) {right_sum = sum; Max_right = j; }} max_value = Left_sum + right_sum; return 0;} int Find_maximum_subarray (int* A, int low, int. High, int& Max_left, int& max_right, int& max_value) {if (hig h = = Low) {max_left = low; Max_right = high; Max_value = A[low]; } else {int mid = (low + high)/2; int tmp_left_low; int Tmp_left_higH int tmp_left_sum; Find_maximum_subarray (A, Low, Mid, Tmp_left_low, Tmp_left_high, tmp_left_sum); int tmp_right_low; int Tmp_right_high; int tmp_right_sum; Find_maximum_subarray (A, mid + 1, high, Tmp_right_low, Tmp_right_high, tmp_right_sum); int tmp_cross_low; int Tmp_cross_high; int tmp_cross_sum; Find_max_crossing_subarray (A, Low, mid, High, Tmp_cross_low, Tmp_cross_high, tmp_cross_sum); if ((tmp_left_sum >= tmp_right_sum) && (tmp_left_sum >= tmp_cross_sum)) {max_left = Tmp_ Left_low; Max_right = Tmp_left_high; Max_value = Tmp_left_sum; } else if ((tmp_right_sum >= tmp_left_sum) && (tmp_right_sum >= tmp_cross_sum)) {MA X_left = Tmp_right_low; Max_right = Tmp_right_high; Max_value = Tmp_right_sum; } else {max_left = Tmp_cross_low; Max_right= Tmp_cross_high; Max_value = Tmp_cross_sum; }} return 0;} Main.cpp#include <iostream> #ifdef __linux#include <stdio.h> #endif # include "Find_max_sub_array.h" Using std::cout;using std::endl;int b[10] = {1, -10, 2, 4, 6, -15, 6, 1, 9,-8};int main () {cout<<endl; int Max_left, max_right, Max_value; Find_maximum_subarray (b, 0, sizeof (b)/sizeof (int)-1, Max_left, Max_right, Max_value); cout<< max_left << "\ t" << max_right << "\ t" << max_value <<endl; cout <<endl; GetChar (); return 0;}
Introduction to Algorithms 4.1 Finding the largest subarray