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. match_parent, wrap_content, 和 fill_parent 区别联系

    fill_parent   -1  The view should be as big as its parent (minus padding). This constant is deprecat ...

  2. c3p0-config.xml模板详解

    c3p0-config.xml模板详解 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.De ...

  3. kali2 install Nessus

    注册: https://www.tenable.com/products/nessus-home 安装: 设置登录用户名,密码,输入注册码:

  4. JS当中利用&&和||简化代码

    ; ){ add_level = ; } ){ add_level = ; } ){ add_level = ; } ){ add_level = ; } else { add_level = ; } ...

  5. A* k短路 学习笔记

    题目大意 n个点,m条边有向图,给定S,T,求不严格k短路 n<=1000 m<=100000 k<=1000 不用LL 分析 A*算法 f(i)表示从S出发经过i到T的估价函数 \ ...

  6. TroubleShoot: SPD 2013 工作流模板问题解决办法

    1. 问题描述: SPD 2013 不能使用2013 工作流模板,在创建过程中,下载更新信息时出现以下错误描述: The server has tried to deliver this messag ...

  7. 【CF1027B】Numbers on the Chessboard(数学)

    题意:给定一个n*n的矩阵与生成矩阵的方式,多次询问同一个方格内(x,y)的数字是多少 题意:浪费人生的矩阵题,找规律 #include<cstdio> #include<cstri ...

  8. luogu 2735 电网 皮克公式

    题目链接 题意 给定一个格点三角形,三个顶点分别为(0,0),(n,m),(p,0),求三角形内部的格点个数. 思路 皮克公式: \[S = \frac{i}{2}+b-1\] \(S\)为三角形面积 ...

  9. 卸载重安firefox

    把firefox完全卸载掉重装: 查看安装的firefox版本: dpkg --get-selections |grep firefox 根据命令结果卸载重装 比如: $ dpkg --get-sel ...

  10. Sphinx的GEO距离搜索 [转载]

    近项目用sphinx做地理搜索,可是结果总是不对,明明很近却搜不到 结果检查sphinx源文件: static inline double sphSqr ( double v ) { return v ...