分治法求最大值与次大值

分治法求最大值与次大值

程序要求

采用分治法求含n个实数的序列中的最大元素和次大元素

函数设计

1void Comp(double arr[],int low,int high,double &max1,double &max2)
	//求求最大值与次大值
2void main()

完整代码

#include<stdio.h>
#include<stdlib.h>

void Comp(double arr[],int low,int high,double &max1,double &max2)
{
	int mid;
	double left_max1,left_max2,right_max1,righr_max2;
	if(high - low < 0)
		//序列左端点小于右端点,程序中断返回
		return;
	if(high - low == 0)
	{
		//左端点等于右端点,只有一个值,即为最大值,次大值记最小
		max1 = arr[high];
		max2 = -1.79e308;
	}
	else if(high - low == 1)
	{
		//序列中刚好有两个实数,即较大值为最大值,另一值为次大值
		if(arr[high] > arr[low])
		{
			max1 = arr[high];
			max2 = arr[low];
		}
		else
		{
			max1 = arr[low];
			max2 = arr[high];
		}
	}
	else
	{
		//序列中实数个数大于两个
		mid = (high + low) / 2;//取中间点
		Comp(arr,low,mid,left_max1,left_max2);//求左部分最大值与次大值,左部边界为low,感谢指正@qq_52672090,@Pythonmather
		Comp(arr,mid + 1,high,right_max1,righr_max2);//求右部分最大值与次大值
		if(left_max1 > right_max1)
		{
			//左边最大值大于右边最大值
			max1 = left_max1;
			if(right_max1 > left_max2)
				//右边最大值大于左边次大值
				max2 = right_max1;
			else
				max2 = left_max2;
		}
		else
		{
			//右边最大值大于左边最大值
			max1 = right_max1;
			if(left_max1 > righr_max2)
				//左边最大值大于右边次大值
				max2 = left_max1;
			else
				max2 = righr_max2;
		}
	}
}

void main()
{
	int input_length,temp;
	double max1,max2;
	printf("请输入实数的个数:\n");
	scanf("%d",&input_length);
	double *input_arr = (double*)malloc(sizeof(double)*input_length);
		//动态创建实数序列
	for(temp = 0;temp < input_length;temp++)
	{
		printf("请输入第 %d 个实数:\n",temp + 1);
		scanf("%lf",&input_arr[temp]);
	}
	printf("\n");
	Comp(input_arr,0,input_length - 1,max1,max2);
	printf("最大值为: %lf \n次大值为: %lf \n",max1,max2);
}

运行示例

运行示例

### 如何用分治法实现寻找数组中的最大值大值 分治法是一种通过将问题分解为更小子问题来解决复杂问题的方法。对于寻找数组中的最大值大值的任务,可以通过递归的方式逐步缩小问题规模并最终得到解。 以下是基于 Python 的一种实现方式: ```python def find_max_and_second_max(arr, left, right): # 如果只有一个元素,则返回该元素作为最大值大值设为 None if left == right: return (arr[left], None) # 如果只有两个元素,则直接比较它们 if right - left == 1: if arr[left] >= arr[right]: return (arr[left], arr[right]) else: return (arr[right], arr[left]) # 将数组分为两部分 mid = (left + right) // 2 # 对左半部分调用函数 max_left, second_max_left = find_max_and_second_max(arr, left, mid) # 对右半部分调用函数 max_right, second_max_right = find_max_and_second_max(arr, mid + 1, right) # 合并结果:找到全局最大值大值 if max_left is not None and max_right is not None: if max_left > max_right: # 左边的最大值 if second_max_left is not None and second_max_left > max_right: return (max_left, second_max_left)[^1] else: return (max_left, max_right)[^1] else: # 右边的最大值 if second_max_right is not None and second_max_right > max_left: return (max_right, second_max_right) else: return (max_right, max_left)[^1] # 测试代码 if __name__ == "__main__": array = [3, 7, 2, 1, 8, 4, 9, 5] maximum, second_maximum = find_max_and_second_max(array, 0, len(array)-1) print(f"最大值: {maximum}, 大值: {second_maximum}") ``` #### 解析 上述方法的核心在于递归地分割数组直到只剩下一个或两个元素的情况,此时可以直接得出局部的最大值大值。随后,在合并阶段通过对左右两侧的结果进行对比,更新当前范围内的最大值大值[^2]。 这种方法的时间复杂度接近于 O(n),因为每递归都会减少一半的数据量处理,并且每层递归只涉及常数级别的操作[^3]。 ---
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值