(经典) K&R的名著<<C程序设计语言>>二分查找
#include<stdio.h> //查找成功则返回所在下标否则返回-1
int binsearch(int A[], int n,int a)
{
int low, high, mid;
low = ;
high = n -;
while ( low <= high) { /// 这里必须是 <=
mid = (low+high) / 2;
if (A[mid] == a)
return mid;
else if(A[mid]<a)
low = mid +;
else
high=mid-;
}
return -;
} int main()
{ int a[]={,,,,};
printf("%d",binsearch(a,,));
}
//二分查找,递归算法
#include <iostream>
#include<stdio.h> using namespace std; int binSearch(int a[],int low,int high,int b)
{
int mid = (high-low)/;
if(a[mid] == b) return mid; if(a[mid] > b) binSearch(a,low,mid-,b); if(a[mid] < b) binSearch(a,mid+,high,b);
} int main()
{ int a[] = {,,,,}; int yy;
yy = binSearch(a,,,);
printf("%d",yy);
return ;
}
(经典) K&R的名著<<C程序设计语言>>二分查找的更多相关文章
- C语言程序设计:二分查找(折半查找)
目录 C语言程序设计:二分查找(折半查找) 1.什么是二分查找 2.二分查找的优点 3.二分查找的缺点 4.二分查找原理 5.源代码实现 6.后话 C语言程序设计:二分查找(折半查找) 1.什么是二分 ...
- C语言二分查找法
参考了C语言中折半查找法(二分法)的实现 二分查找算法(C语言实现) 先附上代码 #include<stdio.h> int BinSearch(int arr[],int len,int ...
- C语言二分查找
#include <stdio.h> /* 二分查找条件: 1.有序序列 2.数据在数组中 */ int baseBinarySearch(int a[],int h,int k) { ; ...
- Go语言 二分查找算法的实现
二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,二分查找算法的前提是传入的序列是有序的(降序或升序),并且有一个目标值. 二分查找的核心思想是将 n 个元素分成大 ...
- 扩展《C程序设计语言》练习2-3程序通用性
最近开始自学C语言,在看K&R的<C程序设计语言>.练习2-3要求写一个函数,将输入的十六进制数字字符串转换成与之等价的整数值,配套答案没有扩展程序的通用性,所以我就稍微改造改造. ...
- 《C++程序设计语言(十周年纪念版)》【PDF】下载
<C++程序设计语言(十周年纪念版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382171 内容简介 <C++程序设计 ...
- ANSI C 与 K&R C
C语言由Dennis M.Ritchie在1973年设计和实现.从那以后使用者逐渐增加.到1978年Ritchie和Bell实验室的另一位程序专家Kernighan合写了著名的<TheC Pro ...
- 《C程序设计语言》(K&R)中文高清非扫描件
<C程序设计语言>(K&R)中文高清非扫描件(带书签目录) 对于某下载东西都要C币的网站无爱了.好不容易找了一个,发出来看会不会帮到别人 附上addr:https://pan. ...
- K&R《C语言》书中的一个Bug
最近在重温K&R的C语言圣经,第二章中的练习题2-2引起了我的注意. 原题是: Write a loop equivalent to the for loop above without us ...
随机推荐
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 11. Container With Most Water(装最多的水 双指针)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 20145202马超 2016-2017-2 《Java程序设计》第7周学习总结
学号 2016-2017-2 <Java程序设计>第X周学习总结 教材学习内容总结 Arrays:用于操作数组的工具类. 里面都是静态方法. asList:将数组变成list集合. 把数组 ...
- jquery 添加列
{field:'action',title:'操作',width:70,align:'center', formatter:function(value,row,index){ if (row.e ...
- 20145314郑凯杰《网络对抗技术》可选实验 shellcode注入与Return-to-libc攻击实验
20145314郑凯杰<网络对抗技术>可选实验 shellcode注入与Return-to-libc攻击实验 1.0 实践内容 Return-to-libc攻击是一种特殊的缓冲区溢出攻击, ...
- Python3基础 print 输出helloworld
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 服务器jupyter配置与ssh远程登录
jupyter 配置 首先安装jupyter,在anaconda套装中已包含,如果安装的是精简版的miniconda则通过conda install jupyter安装. 生成配置文件 jupyter ...
- 【源码学习之spark core 1.6.1 各种部署模式所使用的的TaskSceduler及SchedulerBackend】
说明:个人原创,转载请说明出处 http://www.cnblogs.com/piaolingzxh/p/5656879.html 未完待续 未完待续
- css输入框的圆角
转载:http://jingyan.baidu.com/article/73c3ce28f0b38fe50343d926.html 1.原理是四张圆角的图片放在四个角上,就是圆角矩形的四个角,但这种方 ...
- virtualbox上,android x86 的分辨率的设置
参考文章: http://stackoverflow.com/questions/6202342/switch-android-x86-screen-resolution 1) 用VBoxManage ...