#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. CheatEngine-内存修改

    0.备注+待完成 //备注 a). 如果有方括号,就是说CE认为找 到了数值的指针了 //待完成 a). 自动导出外挂 b). 菜单栏中"表单"下的lua是做什么用的 c). CE ...

  2. postgresql安装,java简单使用postgresql

    一 整合 由于本人的学过的技术太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的github项目地址 https://github.com/247292980/spri ...

  3. Unity3D C# 学习List数据类型的使用

    List<T>类是ArrayList 类的泛型等效类. 该类使用大小可按需动态增加的数组实现 泛型的好处: 它为使用 c#语言编写面向对象程序增加了极大的效力和灵活性.不会强行对值类型进行 ...

  4. 【踩坑】遇到 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 报错

    今天在重做 iblog 客户端时,测试接口情况,发现了 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  5. WPF动画的几种模式

    最近在用WPF做简单动画,以下是几点经验总结: 1. 使用DispatcherTimer做动画 VB6的年代大家就用Timer做动画了,不用多解释,这个DispatcherTimer和本身的Timer ...

  6. CF1182E Product Oriented Recurrence

    思路: fn = can * f1xn * f2yn * f3zn, 首先dp计算指数部分an = an-1 + an-2 + an-3 + 2 * n - 6, 而an-1 = an-2 + an- ...

  7. JAVA中日期格式转换各个字母代表含义

    G  Era 标志符  Text  AD  y  年  Year  1996; 96  M  年中的月份  Month  July; Jul; 07  w  年中的周数  Number  27  W  ...

  8. Linux、命令ps 各字段意思

    参数: -A :所有的进程均显示出来,与 -e 具有同样的效用: -a : 显示现行终端机下的所有进程,包括其他用户的进程: -u :以用户为主的进程状态 : x :通常与 a 这个参数一起使用,可列 ...

  9. 在SQL中查看文件组中有哪些表

    SELECT o.[name], o.[type], i.[name], i.[index_id], f.[name] FROM sys.indexes i INNER JOIN sys.filegr ...

  10. HDU 3351 Seinfeld 宋飞正传(水)

    题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...