/// <summary>
/// 算法MaxMin
/// 输入n个整数元素的数组A[0...n-1]
/// 输出(max,min),A中的最大元素和最小元素
/// 时间:2010-07-23 1:47
/// </summary>
/// <param name="A">数组A</param>
/// <param name="low">最小下标</param>
/// <param name="high">最大下标</param>
/// <param name="max">最大值</param>
/// <param name="min">最小值</param>
public static void MaxMin(int[] A, int low, int high, ref int max, ref int min)
{
int mid, max1, min1, max2, min2;
max1 = min1 = max2 = min2 = 0;
if (high - low == 1 || high - low == 0)
{
max = A[low] > A[high] ? A[low] : A[high];
min = A[low] < A[high] ? A[low] : A[high];
}
else
{
mid = (high + low) / 2;
MaxMin(A, low, mid, ref max1,ref min1);
MaxMin(A, mid + 1, high,ref max2,ref min2);
max = max1 > max2 ? max1 : max2;
min = min1 < min2 ? min1 : min2;
}
}
分治策略返回数组中最大最小值
最新推荐文章于 2022-07-13 10:12:37 发布