数据结构-二分查找(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;
}
数据结构-二分查找(Binary Search)的更多相关文章
- STL之二分查找 (Binary search in STL)
STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...
- 【转】STL之二分查找 (Binary search in STL)
Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...
- LeetCode 704. 二分查找(Binary Search)
704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...
- 二分查找(binary search)
二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...
- [Swift]LeetCode704. 二分查找 | Binary Search
Given a sorted (in ascending order) integer array nums of nelements and a target value, write a func ...
- Go 数据结构--二分查找树
Go 数据结构--二分查找树 今天开始一个Go实现常见数据结构的系列吧.有时间会更新其他数据结构. 一些概念 二叉树:二叉树是每个节点最多有两个子树的树结构. 完全二叉树:若设二叉树的高度为h,除第 ...
- 算法与数据结构基础 - 折半查找(Binary Search)
Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...
- LeetCode编程训练 - 折半查找(Binary Search)
Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...
- 算法与数据结构基础 - 二叉查找树(Binary Search Tree)
二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...
随机推荐
- JVM基础知识2 垃圾收集器与内存分配策略
如何判断堆中的哪些对象可以被回收 主流的程序语言都是使用根搜索算法(GC Roots Tracing)判定对象是否存活 基本思路是:通过一系列名为“GC Roots”的对象作为起点,从这些节点开始向下 ...
- 打印机设置dns
1.登录打印机网页:打印机的ip 2.点击网络--网络标识--设置DNS
- hibernate课程 初探单表映射2-2 hibernate常用配置
1 hibernate.cfg.xml常用配置: show_sql 控制台打印sql format_sql 控制台将sql排版 hbm2ddl.auto: create 删除表结构,重新建表并插值 u ...
- 零基础逆向工程31_Win32_05_提取图标_修改标题
在程序中使用图标 1.加载图标 HICON hIcon; hIcon = LoadIcon (hAppInstance, MAKEINTRESOURCE (IDI_ICON)); hAppInstan ...
- 《Python高效开发实战》实战演练——基本视图3
在完成Django项目和应用的建立后,即可以开始编写网站应用代码,这里通过为注册页面显示一个欢迎标题,来演示Django的路由映射功能. 1)首先在djangosite/app/views.py中建立 ...
- display:table的几个用法(元素平分宽度,垂直居中)
DIV+CSS的布局已经让表格布局几乎很少用到,除非表格语义性很强的情况. display:table解决了一部分需要使用表格特性但又不需要表格语义的情况, 尤其是DIV+CSS很不方便解决的问题,比 ...
- 用sql语句按周、按月、按季、按年统计
--按mySql语法统计按周,月,季,年.income为合计的价格字段,createDate为交易时间. select sum(income)as revenue,week(createDate) a ...
- 美国移民局的I797表原件和I129表是什么呢
I-129表,Petition for a Non-immigrant Worker,即非移民工作许可申请表I797 表 ,Original L1-1A approval notice L1签证批准通 ...
- jQuery_3_过滤选择器
过滤选择器(过滤器)类似于CSS3里的伪类,包含 1. 基本过滤器 2. 内容过滤器 3. 可见性过滤器 4. 子元素过滤器 5. 其他方法 一. 基本过滤器 过滤器名 jQuery语法 注释 :f ...
- TP5.0:的安装与配置
在网址中输入:localhost/安装TP5的文件夹/public/ 入口文件位置:public/index.php: 最新版本中,新建的文件夹是没有模型和视图的,需要自行添加没有的文件: 添加前: ...