2020-12-25

二分查找

1.二分查找基本概念:

二分查找又称折半查找,折半查找就是使有序数列不断缩小为原来的一半,直到找到该元素或折半区域的首元素位置高于尾元素位置为止
输入10个有序的数字和想要查找的关键字,然后用折半查找,找到关键字在数组中的位置

2.二分查找的一般步骤:

折半查找是一种高效的查找方法,该方法可以明显减少比较次数,提高查找效率,但是,折半查找的先决条件是查找表中的元素必须是有序的

首先,必须设定3个变量low,mid,high,分别保存数组元素的开始,中间和结尾的序号,假定有10个元素,则令low=0,high=9,mid=(low+high)/2=4
然后进行以下判断:
(1)如果序号为mid的值与要查找的元素x相等,表示找到了数据,返回对应的序号mid
(2)如果x<a[mid],表示要查找的数据位于low与mid-1序号之间,就不需要查找mid与high之间的元素了,因此,将变量high的值定为mid-1,重新查找low与mid-1之间的数据
(3)如果a[mid]>x,说明要查找的元素位于mid+1与high之间,low=mid+1,重新查找mid+1与high之间的数据
(4)逐步循环如果到low>high时,还未找到目标,就说明数组中无此数据
折半查找的基本思想就是将数列按有序化排列,查找过程中采用跳跃方式查找,即先以有序数组的中间位置为比较对象,如果要找的元素小于该元素,则将待查序列缩小为左半部分,否则为右半部分,通过一次比较,将区间缩小一半

代码部分如下:

#include<stdio.h>
int search(int a[], int n, int x)
{
	int low, mid, high;
	low = 0;
	high = n - 1;
	while (low <= high)
	{
		mid = (low + high) / 2;
		if (a[mid] == x)
			return mid + 1;
		else if (a[mid] > x)
			high = mid - 1;
		else
			low = mid + 1;
	}
	return 0;
}
int main()
{
	int i, x, z;
	int a[10];
	printf("请输入10个有序数字,并用分号隔开:");
	for ( i = 0; i <10 ; i++)
	{
		scanf_s("%d", &a[i]);//为有序数组赋值
	}
	scanf_s("%d", &x);
	z = search(a, 10, x);
	if (z)
		printf("您要查找的数%d在数组中的位置是第%d个元素\n", x, z);
	else
		printf("您要查找的元素%d不在数组中\n", x);
	return 0;
}

3.二分查找的优缺点:

二分查找的优点:平均查找长度小于log2n,即经过一次比较,缩小一半查找范围,经过log2n次比较即可完成折半查找
二分查找的缺点:因为要求数列必须是有序的,所以查找数列必须有序,而对所有数据元素按大小排列是非常费事的操作,另外,顺序存储结构的插入,删除操作不方便

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
func (c *cAsset) CreatComponent(r *ghttp.Request) { var req *v1.CreateComponentReq if err := r.Parse(&req); err != nil { r.Response.WriteJson(g.Map{ "code": 1, "msg": err.Error(), }) } createRequest := &creativecomponent.CreateRequest{ AdvertiserID: req.AdvertiserID, } res, err := service.Asset().Create(createRequest) if err != nil { r.Response.WriteJson(g.Map{ "code": 2, "msg": err.Error(), }) } r.Response.WriteJson(res) }这段代码creativecomponent.CreateRequest的过滤条件为type CreateRequest struct { // AdvertiserID 广告主ID AdvertiserID uint64 `json:"advertiser_id,omitempty"` // ComponentInfo 组件信息 ComponentInfo *ComponentInfo `json:"component_info,omitempty"` },其ComponentInfo为type ComponentInfo struct { // ComponentID 组件ID ComponentID model.Uint64 `json:"component_id,omitempty"` // ComponentType 组件类型 ComponentType enum.ComponentType `json:"component_type,omitempty"` // ComponentName 组件名称。长度小于等于20。一个文长度为2 ComponentName string `json:"component_name,omitempty"` // ComponentData 组件详细信息。不同的component_type对应的值不同,具体的结构见创建或更新接口定义 ComponentData ComponentData `json:"component_data,omitempty"` // CreateTime 创建时间。格式"2020-12-25 15:12:08" CreateTime string `json:"create_time,omitempty"` // Status 组件审核状态。 Status enum.ComponentStatus `json:"status,omitempty"` }想要把ComponentInfo作为参数放到createRequest,该怎么做?请详一点
07-20
要将ComponentInfo作为参数放入createRequest,你可以按照以下步骤进行修改: 首先,你需要在CreateComponentReq结构体添加一个ComponentInfo字段,用于接收组件信息,如下所示: ```go type CreateComponentReq struct { AdvertiserID uint64 `json:"advertiser_id" v:"required" dc:"广告主id"` ComponentInfo *ComponentInfo `json:"component_info,omitempty"` } ``` 接下来,在CreateRequest结构体将ComponentInfo字段的类型更改为*ComponentInfo,如下所示: ```go type CreateRequest struct { AdvertiserID uint64 `json:"advertiser_id,omitempty"` ComponentInfo *ComponentInfo `json:"component_info,omitempty"` } ``` 然后,你可以根据实际需求创建一个ComponentInfo结构体,并将其作为参数传递给createRequest。以下是示例代码: ```go func (c *cAsset) CreatComponent(r *ghttp.Request) { var req *v1.CreateComponentReq if err := r.Parse(&req); err != nil { r.Response.WriteJson(g.Map{ "code": 1, "msg": err.Error(), }) } // 创建ComponentInfo结构体 componentInfo := &creativecomponent.ComponentInfo{ ComponentID: model.Uint64(123), // 组件ID ComponentType: enum.ComponentType("example"), // 组件类型 ComponentName: "Example Component", // 组件名称 // 具体的组件详细信息,根据实际情况进行填充 ComponentData: creativecomponent.ComponentData{ // ... }, CreateTime: "2022-01-01 10:00:00", // 创建时间 Status: enum.ComponentStatus("pending"), // 组件审核状态 } createRequest := &creativecomponent.CreateRequest{ AdvertiserID: req.AdvertiserID, ComponentInfo: componentInfo, // 将ComponentInfo作为参数赋值给createRequest } res, err := service.Asset().Create(createRequest) if err != nil { r.Response.WriteJson(g.Map{ "code": 2, "msg": err.Error(), }) } r.Response.WriteJson(res) } ``` 在上述示例,我们创建了一个ComponentInfo结构体,并将其作为参数赋值给createRequest的ComponentInfo字段。你可以根据实际情况填充ComponentInfo结构体的字段。请确保在填充ComponentData字段时,根据组件类型进行相应的赋值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 落禅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值