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. 浅析win32 Win64 x86 x64 区别 及Eclipse启动报Java was started but returned exit code=13 错误

    win32.x86_64是64位 X86就是  32位系统 X64 就是64位系统 最好记得方法就是带有64的就是64位,其余都是32位 为什么要讲这个呢? 如果是绿色版本的eclipse,在打开ec ...

  2. Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)

    题目:给定一个字符串S(主串),一个字符串数组words,其中的字符串的长度相同.找到所有的子串位置,要求是words中字符串的一个连接: 举例: For example, given:s: &quo ...

  3. 微信小程序开发 -- 手机振动

    wx.vibrateLong(OBJECT) wx.vibrateLong(OBJECT) 方法使手机发生较长时间的振动(400ms) OBJECT参数说明: 参数名 类型 必填 说明 success ...

  4. [python][oldboy]字符串 format

    #coding=utf8 def format(self, *args, **kwargs): # known special case of str.format """ ...

  5. Map容器——TreeMap及常用API,Comparator和Comparable接口

    TreeMap及常用API ①   TreeMap类通过使用红黑树实现Map接口; ②   TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③   不像散列(HashMap), ...

  6. 刷题总结———长跑路径(ssoj1982)

    题目: 给定一个无向图···求特定几个点中两两间的最短路中的最小值····其中1≤N,M≤100000:T≤5:1≤K≤n:1≤边长≤100000,T为一个测试点的测试数··k为测试点数量 题解: 我 ...

  7. Quotes in shell(bash, csh)

    There are three kinds of quote: single quote('), double quote("), backslash(\), how shell expla ...

  8. Best Coder Lotus and Characters

     Lotus and Characters  问题描述 Lotus有nn种字母,给出每种字母的价值以及每种字母的个数限制,她想构造一个任意长度的串. 定义串的价值为:第1位字母的价值*1+第2位字母的 ...

  9. 如何应用r.js对requirejs下的js代码合并

    1.在根目录新建build.js ({ baseUrl:'js', paths:{ jquery:'static/jquery-1.10.2.min', underscore:'static/unde ...

  10. 天梯赛 - L2-003 月饼

    题目链接:https://www.patest.cn/contests/gplt/L2-003 现在只能做水题~ #include <iostream> #include <cstd ...