代码

#include <stdio.h>

int binarySearch(int iList[], int iNum, int iX, int * pPos)
{
if(NULL == pPos)
{
return -;
}
int i = , j = iNum;
while(i < j)
{
printf("from [%d] to [%d]\n", i, j);
int iPos = (i + j) / ;
int iMid = iList[iPos];
if(iX == iMid)
{
*pPos = iPos;
return ;
}
else if (iX > iMid)
{
i = iPos + ;
}
else
{
j = iPos - ;
}
}
if(iList[i] == iX)
{
*pPos = i;
return ;
}
*pPos = -;
return -;
} int main(int argc ,char * argv[])
{
int iList[] = {, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
int iPos = ;
int iNum = ; int iX = ;
binarySearch(iList, iNum, iX, &iPos);
printf("[%d] @ position [%d]\n\n", iX, iPos); iX = ;
binarySearch(iList, iNum, iX, &iPos);
printf("[%d] @ position [%d]\n\n", iX, iPos); iX = ;
binarySearch(iList, iNum, iX, &iPos);
printf("[%d] @ position [%d]\n\n", iX, iPos); iX = ;
binarySearch(iList, iNum, iX, &iPos);
printf("[%d] @ position [%d]\n\n", iX, iPos); iX = -;
binarySearch(iList, iNum, iX, &iPos);
printf("[%d] @ position [%d]\n\n", iX, iPos); iX = ;
binarySearch(iList, iNum, iX, &iPos);
printf("[%d] @ position [%d]\n\n", iX, iPos); return ;
}

编译

$ g++ -o binarySearch binarySearch.cpp

运行

$ ./binarySearch
from [] to []
from [] to []
from [] to []
from [] to []
[] @ position [] from [] to []
from [] to []
from [] to []
from [] to []
[] @ position [-] from [] to []
from [] to []
from [] to []
from [] to []
[] @ position [] from [] to []
from [] to []
from [] to []
from [] to []
from [] to []
[] @ position [] from [] to []
from [] to []
from [] to []
from [] to []
[-] @ position [-] from [] to []
from [] to []
from [] to []
from [] to []
from [] to []
[] @ position [-]

再见……

纪念逝去的岁月——C/C++二分查找的更多相关文章

  1. 纪念逝去的岁月——C++实现一个队列(使用类模板)

    1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...

  2. 纪念逝去的岁月——C++实现一个栈(使用类模板)

    这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...

  3. 纪念逝去的岁月——C++实现一个栈

    1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...

  4. 纪念逝去的岁月——C/C++排序二叉树

    1.代码 2.运行结果 3.分析 1.代码 #include <stdio.h> #include <stdlib.h> typedef struct _Node { int ...

  5. 纪念逝去的岁月——C/C++快速排序

    快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  6. 纪念逝去的岁月——C/C++交换排序

    交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  7. 纪念逝去的岁月——C/C++选择排序

    选择排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  8. 纪念逝去的岁月——C/C++冒泡排序

    冒泡排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  9. 纪念逝去的岁月——C/C++字符串回文

    判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...

随机推荐

  1. LSM-Tree (BigTable 的理论模型)(转)

    Google的BigTable架构在分布式结构化存储方面大名鼎鼎,其中的MergeDump模型在读写之间找到了一个较好的平衡点,很好的解决了web scale数据的读写问题. MergeDump的理论 ...

  2. VS2013缺少报表工具

    问题1:缺少报表设计工具--即rdlc无法打开设计器 原因:缺少SQL Server Data Tools(SSDT)工具 解决:安装ssdt即可 SSDT下载地址:https://msdn.micr ...

  3. HDU 4005 The war Tarjan+dp

    The war Problem Description   In the war, the intelligence about the enemy is very important. Now, o ...

  4. 【vijos】P1514天才的记忆

    描述 从前有个人名叫W and N and B,他有着天才般的记忆力,他珍藏了许多许多的宝藏.在他离世之后留给后人一个难题(专门考验记忆力的啊!),如果谁能轻松回答出这个问题,便可以继承他的宝藏.题目 ...

  5. java线程之——synchronized的注意细节

    我在学习synchronized的时候,十分好奇当一个线程进入了一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 然后就做了个实验(实验代码最后贴出),最后得到了如下 ...

  6. python学习第三天

    小结: 总体上,python是解释型语言,开源比较好,速度较慢,装逼神器,解释器较常用的是CPython,安装后python进入运行环境 exit()退出 第一个hello world : print ...

  7. LoadRunner 脚本学习 -- 使用动态链接库

    DLL = Dynamic Link Library DLL最重要的一个特点就扩展应用程序的特性. 再强大的工具也有不是万能的,通过调用动态库的方法极大地增强loadrunner的功能.当你在用loa ...

  8. 网上下载的CHM帮助文件打不开的解决办法。

    我的机器 装的是 Windows server 2008 操作系统.他的安全性比较高. 我在网上下载了一个 CHM 帮助文档.结果打不开. 现象: 打开时 ,提示 安全警告, 提示:来自Interne ...

  9. JQuery学习之其他

    1.noConflict()方法:释放会$标识符的控制,这样其他也用$的脚本就可以使用它了 **全名代替简写的方法使用jQuery $.noConflict(); jQuery(document).r ...

  10. Codeforces 581F Zublicanes and Mumocrates(树形DP)

    题目大概说有一棵树要给结点染色0或1,要求所有度为1的结点一半是0一半是1,然后问怎么染色,使两端点颜色不一样的边最少. dp[0/1][u][x]表示以u结点为根的子树中u结点是0/1色 且其子树有 ...