Binary search, binarysearch

Source: Internet
Author: User

Binary search, binarysearch

  1. // Configure //--------------------------------------------------------------------------------------------------------------------
  2. // <Copyright company = "Chimomo's Company" file = "Program. cs">
  3. // Respect the work.
  4. // </Copyright>
  5. // <Summary>
  6. // The binary search (not recursive ).
  7. // [Prerequisites for searching in half]:
  8. // 1. The sequence to be searched must adopt a sequential storage structure.
  9. // 2. The sequence to be searched must be ordered by the keyword size.
  10. /// </Summary>
  11. // Configure //--------------------------------------------------------------------------------------------------------------------
  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. /// Search for elements whose values are key (non-recursive search) in an ordered array a with a length of n ).
  30. /// </Summary>
  31. /// <Param name = "a">
  32. /// The array to be searched.
  33. /// </Param>
  34. /// <Param name = "key">
  35. /// Target element.
  36. /// </Param>
  37. /// <Param name = "n">
  38. /// Array length.
  39. /// </Param>
  40. /// <Returns>
  41. /// If the target element is found, the subscript of the target element in the array is returned; otherwise,-1 is returned.
  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. Time Complexity: O (log2n)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.