1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
#include <string>
using namespace std;
template <class ElementType>
ElementType maxArray(ElementType array[], int start, int end);
int main ()
{
int num[] = {5, 6, 7, 897, 17, 10, 15, 3};
cout << maxArray(num, 0, 7) << endl;
char str[] = {'a', 'b', 'c', 'h', 'e', 'f', 'g'};
cout << maxArray(str, 0, 6) << endl;
}
template <class ElementType>
ElementType maxArray(ElementType array[], int start, int end)
{
if (start == end)
{
return array[start];
}
int mid = (start + end) /2;
int left = maxArray(array, start, mid);
int right = maxArray(array, mid+1, end);
if (array[left] > array[right])
{
return array[left];
}
else
{
return array[right];
}
}
| |