数据结构-二分查找(Binary Search)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define true 1
#define false 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define OPSETSIZE 7
#define MAXQSIZE 100
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))



typedef int ElemType;
typedef struct
{
    ElemType *elem;
    int  length;
} SSTable;


int CreatSST(SSTable *sst, int n, ElemType key);
int Search_Seq(SSTable *ST, ElemType key);
int Search_Bin(SSTable ST, ElemType key);
int sort(SSTable *s, int n);

int main()
{
    printf("Please input the number of the static search table:\n");
    int n, i;
    scanf("%d",&n);
    printf("Please input a table:\n");
    SSTable sst;
    ElemType key;
    CreatSST(&sst, n, key);
    for(i= 0; i< n; i++)
        scanf("%d",&sst.elem[i+1]);
    printf("The static search table is:\n");
    for(i= 0; i< n ;i++)
        printf("%d%c", sst.elem[i+1], i== n- 1? '\n': ' ');
    printf("Please input the element to search:\n");
    scanf("%d", &key);
    printf("The position is %d\n", Search_Seq(&sst, key));
    printf("Next,using binary search->\n");
    printf("After sort,the static search table is:\n");
    sort(&sst, n);
    for(i= 0; i< n ;i++)
        printf("%d%c", sst.elem[i+1], i== n- 1? '\n': ' ');
    printf("Please input the element to search:\n");
    scanf("%d", &key);
    printf("The position is: %d\n", Search_Bin(sst, key));
    return 0;
}


int CreatSST(SSTable *sst, int n, ElemType key)
{
    int i= 0;
    sst->elem = (ElemType*)malloc((n + 1) * sizeof(ElemType));
    if(!sst->elem)
        exit(OVERFLOW);
    sst->elem[0] = key;
    sst->length = n;
    if(!sst->length)
        exit(OVERFLOW);
    return OK;
}

int Search_Seq(SSTable *ST, ElemType key)
{
    int i;
    ST->elem[0] = key;
    for (i=ST->length; !EQ(ST->elem[i], key); --i);
    return i;
}

int Search_Bin(SSTable ST,ElemType key)
{
    int low=1;
    int high=ST.length;
    while (low<=high)
    {
        int mid=(low+high)/2;
        if (EQ(key,ST.elem[mid])) return mid;
        else if (LT(key, ST.elem[mid])) high=mid-1;
        else low=mid+1;
    }
    return 0;
}

int sort(SSTable *s, int n)
{
    int i, j;
    ElemType temp;
    for(i = 1; i<= n; i++)
        for(j = 1; j<= n- i; j++)
            if(s->elem[j] > s->elem[j+1])
                temp = s->elem[j], s->elem[j] = s->elem[j+1], s->elem[j+1] = temp;
}


转载于:https://www.cnblogs.com/chinashenkai/p/9451399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二分查找算法(Binary Search Algorithm)是一种在已排序数组中查找特定元素的算法。算法通过将目标值与数组的中间元素进行比较,并根据比较结果将搜索范围缩小一半。若目标值等于中间元素,则找到目标值;若目标值小于中间元素,则在左半部分继续查找;若目标值大于中间元素,则在右半部分继续查找。重复以上步骤直到找到目标值或搜索范围为空。 下面是用中文编写的二分查找算法search_bin的示例代码: ```python def search_bin(arr, target): left = 0 right = len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid if arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 ``` 在以上示例代码中,search_bin函数接收一个已排序的数组arr和目标值target作为参数,返回目标值在数组中的索引。算法开始时,定义左边界left为数组的第一个元素的索引,右边界right为数组的最后一个元素的索引。使用while循环,在左边界小于等于右边界的条件下进行查找。 在每次循环中,计算中间元素的索引mid,并与目标值进行比较。如果中间元素等于目标值,表示找到目标值,返回中间元素的索引。如果中间元素小于目标值,说明目标值在右半部分,将左边界更新为mid+1,继续查找右半部分。如果中间元素大于目标值,说明目标值在左半部分,将右边界更新为mid-1,继续查找左半部分。 当左边界大于右边界时,表示搜索范围为空,说明未找到目标值,返回-1表示搜索失败。 经过以上步骤,当目标值存在于已排序数组中时,二分查找算法能够高效地找到目标值的索引。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值