数据结构-二分查找(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)或递归 ...
随机推荐
- OpenStack git cmd
文件流转的三个工作区域:Git 的工作目录,暂存区域,以及本地仓库. 基本的 Git 工作流程如下: 在工作目录中修改某些文件. 对修改后的文件进行快照,然后保存到暂存区域. 提交更新,将保存在暂存区 ...
- $(document).ready(function() { });
l $(document).ready(function() { }); 初始化jsp
- 【C#】C#委托使用详解(Delegates)
摘要 委托是C#编程一个非常重要的概念,也是一个难点.本文将系统详细讲解委托. 1. 委托是什么? 其实,我一直思考如何讲解委托,才能把委托说得更透彻.说实话,每个人都委托都有不同的见解,因为看问题的 ...
- Python 为threading.Thread添加 terminate
import threading import inspect import ctypes def _async_raise(tid, exc_type): """rai ...
- 360或其他双核浏览器下在兼容模式用chrome内核渲染的方法
<meta name="renderer" content="webkit"> <meta http-equiv="X-UA-COM ...
- Backbone源码风格
代码风格: 一.自执行匿名函数创建执行环境 var root = this; root保存全局执行环境的指针.浏览器端为window对象 二.依赖库 (1).underscore 如果bac ...
- JS中关于clientWidth offsetWidth scrollWidth 等的区别
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- 3D向2D投影
http://blog.sina.com.cn/s/blog_536e0eaa0100jn7j.html
- 05、Win7上openSSH的安装与配置
05.Win7上openSSH的安装与配置 1.概述 linux上的ssh命令在网络通信场景下非常方便.现在windows也支持ssh方式和远程主机进行访问.如果只是使用ssh简单的访问功能,就需要很 ...
- Django Form 表单
Form 表单功能 生成HTML表单元素检查表单元素的合法性验证如果错误,重复显示表单数据类型转换 Form相关的对象 Widget 渲染成HTML元素的工具Field Form对象中的一个字段For ...