Foundation: Binary Search
/* Binary search.
*
* Implementation history:
* 2013-10-5, Mars Fu, first version.
*/ /* [Binary Search Algorithm]
* Published by John Mauchly(1946) and D.H.Lehmer(1960).
*
* [Uniform Binary Search Algorithm]
* Published by D.E.Knuth(1963) and A.K.Chandra(1971).
*/ #include "stdafx.h"
#include "binary_search.h"
#include "quicksort.h" int
do_binary_search(void *key, void *src, int src_sz, int n, cmp_func cmp)
{
int li,ri,i;
char *p; F_S(); if (key == NULL || src == NULL || cmp == NULL) return 0;
if (src_sz <= 0 || n <= 0) return 0; li = 1;
ri = n;
while (li <= ri) { i = ((li + ri) >> 1); p = (char*)src + (i-1) * src_sz;
if (cmp(key, p) < 0) {
ri = i - 1;
}
else if (cmp(key, p) > 0) {
li = i + 1;
}
else {
goto END;
}
} return 0; END:
F_E();
return i;
} int
do_uniform_binary_search(void *key, void *src, int src_sz, int n, cmp_func cmp)
{
int i,m;
char *p; F_S(); if (key == NULL || src == NULL || cmp == NULL) return 0;
if (src_sz <= 0 || n <= 0) return 0; i = (n + 1) / 2;
m = n / 2;
while (1) { p = (char*)src + (i-1) * src_sz;
if (cmp(key, p) < 0) {
if (m == 0) return 0;
i -= ((m + 1) / 2);
}
else if (cmp(key, p) > 0) {
if (m == 0) return 0;
i += ((m + 1) / 2);
}
else {
goto END;
} m = m / 2;
} return 0;
END:
F_E();
return i;
} int
do_uniform_binary_search_2(void *key, void *src, int src_sz, int n, cmp_func cmp)
{
int i,j,k,h,m;
char *p;
int n2; F_S(); if (key == NULL || src == NULL || cmp == NULL) return 0;
if (src_sz <= 0 || n <= 0) return 0; /* formula:
* h(j) = (n + 2^(j-1)) / (2^j), where 1 <= j <= (int)lg(n) + 2.
*/
n2 = 2;
m = (int)(log((double)n) / log((double)n2)) + 2; for (i = (n + 1) / 2, j = 1, k = 2; j < m; ++j, k <<= 1) { h = (n + k) / (k << 1); p = (char*)src + (i-1) * src_sz;
if (cmp(key, p) < 0) {
if (h == 0) return 0;
i -= h;
}
else if (cmp(key, p) > 0) {
if (h == 0) return 0;
i += h;
}
else {
goto END;
}
} return 0;
END:
F_E();
return i;
} #ifdef BINARY_SEARCH_DEBUG int
int_cmp(void* lv, void* rv)
{
int tmp_lv, tmp_rv; tmp_lv = *(int*)lv;
tmp_rv = *(int*)rv; return (tmp_lv - tmp_rv);
} static void
exchange_int_item(void* lv, void* rv)
{
int tmp_lv, tmp_rv; tmp_lv = *(int*)lv;
tmp_rv = *(int*)rv; *(int*)lv = *(int*)rv;
*(int*)rv = tmp_lv;
} int
main(int argc, char* argv[])
{
int i;
int cnt;
int ret;
int int_items[16] = { 503, 87, 512, 61, 908, 170, 897, 275,
653, 426, 154, 509, 612, 677, 765, 703
};
int key; debug("[Debug binary search].. \r\n"); cnt = sizeof(int_items)/sizeof(int_items[0]); debug("src database:\r\n----\r\n");
for (i = 0; i < cnt; ++i) {
debug("%d ", int_items[i]);
}
debug("\r\n"); ret = do_quick_sort((void*)int_items, sizeof(int), cnt,
int_cmp, exchange_int_item, NULL);
if (!ret) {
debug("failed. \r\n");
goto END;
} debug("src database sorted:\r\n----\r\n");
for (i = 0; i < cnt; ++i) {
debug("%d ", int_items[i]);
}
debug("\r\n"); debug("\r\n----\r\n");
key = int_items[0];
debug("search key %d... \r\n", key);
ret = do_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search_2(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n"); debug("\r\n----\r\n");
key = int_items[cnt-1];
debug("search key %d... \r\n", key);
ret = do_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search_2(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n"); debug("\r\n----\r\n");
key = int_items[cnt/2];
debug("search key %d... \r\n", key);
ret = do_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search_2(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n"); debug("\r\n----\r\n");
key = 12345;
debug("search key %d... \r\n", key);
ret = do_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n");
ret = do_uniform_binary_search_2(&key, int_items, sizeof(int), cnt, int_cmp);
if (!ret) debug("not found.\r\n"); debug("\r\n"); debug("[Debug binary search]..done. \r\n"); END:
while (1);
return 1;
} #endif /* BINARY_SEARCH_DEBUG */
#ifndef __BINARY_SEARCH_H__
#define __BINARY_SEARCH_H__ #define BINARY_SEARCH_DEBUG typedef int(*cmp_func)(void*,void*); int do_binary_search(void *key, void *src, int src_sz, int n, cmp_func cmp);
int do_uniform_binary_search(void *key, void *src, int src_sz, int n, cmp_func cmp);
int do_uniform_binary_search_2(void *key, void *src, int src_sz, int n, cmp_func cmp); #endif /* __BINARY_SEARCH_H__ */
#pragma once #include <windows.h>
#ifdef _WIN32
#define msleep(x) Sleep(x)
#endif #include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h> #define MY_DEBUG
#ifdef MY_DEBUG
#define debug printf
#else
#define debug(x,argc, __VA_ARGS__) ;
#endif /* MY_DEBUG */ #define F_S() debug("[%s]..\r\n", __FUNCTION__)
#define F_E() debug("[%s]..done. \r\n", __FUNCTION__)
Enjoy~
Mars
October 5, 2013
Foundation: Binary Search的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- 简单说明Python中的装饰器的用法
简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下 装饰器对与 ...
- 设计模式UML图
1.简单工厂模式 2.工厂模式 工厂模式与简单工厂模式的不同在于,每个操作类都有自己的工厂,而且把逻辑判断交给了客户端,而简单工厂的逻辑判断在工厂类里边,当增加新的操作类时,简单工厂需要修改工厂类,而 ...
- css3中-webkit-text-size-adjust详解
1.当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 html{-webkit-text-size-adjust:none;} 2.-webk ...
- 编写高效SQL最佳实践
编写高效 SQL 语句的最佳实践 秦玮, 高级软件工程师, IBM 王广成, 软件工程师, IBM 王韵婷, 高级软件工程师, IBM 简介: 本文列举了一些在编写 SQL 查询语句时可能导致 DB2 ...
- onresize的定义方式
1.直接在html中定义如<body onresize="doResize()"/> 2.直接给onresize赋值给window和body的onresize赋值如wi ...
- IDEA中添加各种依赖pom.xml文件内容
刚实习的小白,今天准备进入项目,纳尼,前辈把框架什么的都搭建好了,默默的抹了一把辛酸泪,刚刚接触自学框架的时候,添加依赖的时候总是各种问题,让前辈发给我之后,才发现人家写的代码相当优美了.下面就是前辈 ...
- Bootstrap_排版_代码
不管使用哪种代码风格,在代码中碰到小于号(<)要使用硬编码“<”来替代,大于号(>)使用“>”来替代 一.单行内联代码 <code>:一般是针对于单个单词或单个句子 ...
- PHP开发套件
Windows系统下开发 环境配置: PHPstudy----立即下载 开发工具: PHPstorm----立即下载 引用一个注册服务器地址:潘田--phpstorm 2016.1注册码 当然推荐大家 ...
- 兄弟连王牌PHP课程送三重豪礼啦!
兄弟连PHP就业办课程送三重豪礼啦! 惊喜一:报名9月23日班级,保障薪资直涨1000元! 9月报名学习,春节后就业,正是企业招聘的黄金高峰期,一年中拿到高薪最好的时节! 惊喜二:兄弟连云课堂900元 ...
- 基于memcached中命令分析函数tokenize_command改造的split函数
今天使用C重构php代码,需要手写一个split函数,于是就模仿memcached中获取用户命令的函数 static size_t tokenize_command(char *command, to ...