#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;
}

数据结构-二分查找(Binary Search)的更多相关文章

  1. STL之二分查找 (Binary search in STL)

    STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...

  2. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  3. LeetCode 704. 二分查找(Binary Search)

    704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...

  4. 二分查找(binary search)

    二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...

  5. [Swift]LeetCode704. 二分查找 | Binary Search

    Given a sorted (in ascending order) integer array nums of nelements and a target value, write a func ...

  6. Go 数据结构--二分查找树

    Go 数据结构--二分查找树 今天开始一个Go实现常见数据结构的系列吧.有时间会更新其他数据结构. 一些概念 二叉树:二叉树是每个节点最多有两个子树的树结构. 完全二叉树:若设二叉树的高度为h,除第 ...

  7. 算法与数据结构基础 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  8. LeetCode编程训练 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  9. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

随机推荐

  1. net core 的Generic Host 之Generic Host Builder

    前言 通用Host(Generic Host) 与 web Host 不同的地方就是通用Host解耦了Http请求管道,使得通用Host拥有更广的应用场景.比如:消息收发.后台任务以及其他非http的 ...

  2. Mybatis的小计

    1连接池 一 我的错误想法 poolMaximumIdleConnections 最大活跃连接数 poolMaximumActiveConnections 最大空闲连接数 我一直以为 空闲是一直存在的 ...

  3. Spring整合Struts2 XML版

    1.jar包 <!--spring配置--> <dependency> <groupId>org.springframework</groupId> & ...

  4. 内容显示分页数字分页 aspx

    此处是aspx里面分页显示,数据层和业务层是由动软生成 当然,我们也可以可以利用listView实现分页ListView(高效分页) public partial class NewList : Sy ...

  5. myBatis执行测试批量删除,出现测试类正常显示,但数据库数据没变

    一般在测试myBatis运行正常,但数据库数据不变时,有可能是SQL语句有问题,检查SQL语句没问题,但数据库依然没变,就说明myBatis方法执行后并未提交到数据库,可尝试在测试类添加   sess ...

  6. hibernate课程 初探单表映射3-1 hibernate单表操作简介

    本章简介: 1 单一主键 2 基本类型 3 对象类型 4 组件属性 5 单表操作CRUD实例

  7. 【作业留存】根据IATF框架,设计的一种中小型企业安全拓扑

  8. COGS 1365. [HAOI2013] 软件安装

    ★★☆   输入文件:haoi13t4.in   输出文件:haoi13t4.out   简单对比时间限制:1 s   内存限制:128 MB Dr.Kong有一个容量为N MB (1 <= N ...

  9. Metasploitable渗透测试实战——Windows漏洞 MS08-067复现

    Ms08-067 攻防环境: 攻击机:kali     ip:198.168.12.212 靶机:Window XP 未打过ms08-067补丁  ip:198.168.12.209

  10. linux 命令——4 mkdir (转)

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...