1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright company="Chimomo's Company" file="Program.cs">
  3. // Respect the work.
  4. // </copyright>
  5. // <summary>
  6. // The binary search (not recursive).
  7. // [折半查找的前提]:
  8. // 1、待查找序列必须採用顺序存储结构。
  9. // 2、待查找序列必须是按keyword大小有序排列。

  10. // </summary>
  11. // --------------------------------------------------------------------------------------------------------------------
  12. namespace CSharpLearning
  13. {
  14. using System;
  15. /// <summary>
  16. /// The program.
  17. /// </summary>
  18. internal class Program
  19. {
  20. /// <summary>
  21. /// Entry point into console application.
  22. /// </summary>
  23. public static void Main()
  24. {
  25. int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  26. Console.WriteLine(BinarySearch(a, 6, 9));
  27. }
  28. /// <summary>
  29. /// 在长度为n的有序数组a中查找值为key的元素(非递归查找)。
  30. /// </summary>
  31. /// <param name="a">
  32. /// 待查找数组。

  33. /// </param>
  34. /// <param name="key">
  35. /// 目标元素。

  36. /// </param>
  37. /// <param name="n">
  38. /// 数组长度。

  39. /// </param>
  40. /// <returns>
  41. /// 若查找到目标元素则返回该目标元素在数组中的下标。否则返回-1。
  42. /// </returns>
  43. private static int BinarySearch(int[] a, int key, int n)
  44. {
  45. int low = 0;
  46. int high = n - 1;
  47. while (low <= high)
  48. {
  49. int mid = (low + high) / 2;
  50. if (a[mid] == key)
  51. {
  52. return mid;
  53. }
  54. if (a[mid] < key)
  55. {
  56. low = mid + 1;
  57. }
  58. else
  59. {
  60. high = mid - 1;
  61. }
  62. }
  63. return -1;
  64. }
  65. }
  66. }
  67. // Output:
  68. /*
  69. 5
  70. */
  71. 时间复杂度:O(log2n)

算法 binary search的更多相关文章

  1. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. 九章算法系列(#2 Binary Search)-课堂笔记

    前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一 ...

  4. 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

    题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...

  5. 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

    [109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...

  6. 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)

    议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...

  7. 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

    [096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...

  8. 算法与数据结构基础 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  9. 算法学习笔记之——priority queue、heapsort、symbol table、binary search trees

    Priority Queue 类似一个Queue,但是按照priority的大小顺序来出队 一般存在两种方式来实施 排序法(ordered),在元素入队时即进行排序,这样插入操作为O(N),但出队为O ...

随机推荐

  1. 令人惊叹的sublime text 3 插件

    1.Chinese​Localization------语言汉化.(新手必备) 2.SublimeTmpl------打开生成模板.(新手必备) 3.SublimeCodeIntel------代码自 ...

  2. 再谈H2的MVStore与MVMap

    对H2的[MVStore]: http://www.cnblogs.com/simoncook/p/5188105.html 这篇文章的补充. 概述 我们通常用的map,比如HashMap Linke ...

  3. python-安装及配置环境变量

    1.python安装十分简单,直接下载与自己电脑位数匹配的python安装包进行安装即可. 这里提供python27的安装包供大家参考. win-32位: 链接: https://pan.baidu. ...

  4. Leetcode27--->Remove Element(移除数组中给定元素)

    题目:给定一个数组array和一个值value,移除掉数组中所有与value值相等的元素,返回新的数组的长度:要求:不能分配额外的数组空间,且必须使用原地排序的思想,空间复杂度O(1); 举例: Gi ...

  5. hdu1599 find the mincost route floyd求出最小权值的环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. 九度oj 题目1452:搬寝室

    题目描述: 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整 ...

  7. Golang遇到的问题记录

    1,windows cmd 结束输入问题 func main() { counts := make(map[string]int) countLines(os.Stdin, counts) fmt.P ...

  8. 设计模式(一)单例模式:实现 Serializable 接口之后的额外操作

    思想: 一个单例类,无论采取哪一种设计(单元素枚举类除外), 一旦间接或者直接实现 Serializable 接口,为了保证单例,就要多增加一点考虑:保证类在反序列化之后能够保证单例. public ...

  9. 论蛋疼的调戏matrix67的首页

    唔,初学js,然后拿matrix67的首页玩玩23333 console 里输入以下这句话 for(var i = 1; i <= 400; i++){var td = document.get ...

  10. 排列计数(bzoj 4517)

    Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...