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. apache 虚拟主机配置(根据不同的域名映射到不同网站)

    最近弄了台香港服务器做测试,Web服务器软件用的是Apache2.2,机器只有一台,ip只有一个,但是想测试几个站点,于是尝试了下Apache的虚拟主机配置.之前已经写过一篇博文了——<Apac ...

  2. python + selenium - selenium简介

    1. 产品简介 selenium 是 基于 web网页的UI自动化测试框架. 1)支持多浏览器操作:ie.chrome.firefox.edge.safaria等 2)跨平台:windows.linu ...

  3. 缓存淘汰算法之LFU

    1. LFU类 1.1. LFU 1.1.1. 原理 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频 ...

  4. Spring配置文件中使用ref local与ref bean的区别

    Spring配置文件中使用ref local与ref bean的区别.在ApplicationResources.properties文件中,使用<ref bean>与<ref lo ...

  5. javascript基础1 语法 点击事件 超链接带点击事件

    javascript ----------------------------------------------------------------------------------------- ...

  6. 【Istio】error initializing configuration '/etc/istio/proxy/envoy-rev0.json': malformed IP address: istio-statsd-prom-bridge

    今天遇到一个问题,istio的组件一直在重启,查看log大概是这个样子 --03T07::.935580Z info Epoch starting --03T07::.936317Z info Env ...

  7. [暑假集训--数论]poj2657 Comfort

    Description A game-board consists of N fields placed around a circle. Fields are successively number ...

  8. input标签不能设置height

    首先input是内联标签(inline) inline元素设置width.height属性无效 可以通过设置display:inline-block ,则内联标签可以设置width和height,但是 ...

  9. HDU 2594 kmp算法变形

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  10. jrebel 激活

    jrebel idea插件激活,亲测可用: 在jrebel server处,写上: http://139.199.89.239:1008/88414687-3b91-4286-89ba-2dc813b ...