学习自:一周刷爆LeetCode,算法大神左神(左程云)耗时100天打造算法与数据结构基…
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int maxArr(int* arr, int L, int R);
int main()
{
int arr[] = {3,2,4,6,1,9};
int lenght = sizeof(arr) / sizeof(arr[0]);
int arrMax = maxArr(arr, 0, lenght - 1);
cout << arrMax << endl;
return 0;
}
int maxArr(int* arr, int L, int R)
{
cout << "one\t" << "L = " << L << "\tR=" << R << endl;
if (L == R)
{
cout << "two\t" << "L = " << L << "\tR=" << R << endl;
return arr[L];
}
int mid = L + ((R - L) >> 1);
int leftMax = maxArr(arr, L, mid);
int rightMax = maxArr(arr, mid + 1, R);
cout << "three\t" << "L = " << L << "\tR=" << R << endl;
return max(leftMax, rightMax);
}
根据打印出来的信息,可以等到运行的顺序图
数字代表运行的顺序
|