演算法 binary search,binarysearch

來源:互聯網
上載者:User

演算法 binary search,binarysearch

  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、待尋找序列必須是按關鍵字大小有序排列。  
  10. // </summary>  
  11. // --------------------------------------------------------------------------------------------------------------------  
  12.   
  13. namespace CSharpLearning  
  14. {  
  15.     using System;  
  16.   
  17.     /// <summary>  
  18.     /// The program.  
  19.     /// </summary>  
  20.     internal class Program  
  21.     {  
  22.         /// <summary>  
  23.         /// Entry point into console application.  
  24.         /// </summary>  
  25.         public static void Main()  
  26.         {  
  27.             int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  28.             Console.WriteLine(BinarySearch(a, 6, 9));  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// 在長度為n的有序數組a中尋找值為key的元素(非遞迴尋找)。  
  33.         /// </summary>  
  34.         /// <param name="a">  
  35.         /// 待尋找數組。  
  36.         /// </param>  
  37.         /// <param name="key">  
  38.         /// 目標元素。  
  39.         /// </param>  
  40.         /// <param name="n">  
  41.         /// 數組長度。  
  42.         /// </param>  
  43.         /// <returns>  
  44.         /// 若尋找到目標元素則返回該目標元素在數組中的下標;否則返回-1。  
  45.         /// </returns>  
  46.         private static int BinarySearch(int[] a, int key, int n)  
  47.         {  
  48.             int low = 0;  
  49.             int high = n - 1;  
  50.             while (low <= high)  
  51.             {  
  52.                 int mid = (low + high) / 2;  
  53.                 if (a[mid] == key)  
  54.                 {  
  55.                     return mid;  
  56.                 }  
  57.   
  58.                 if (a[mid] < key)  
  59.                 {  
  60.                     low = mid + 1;  
  61.                 }  
  62.                 else  
  63.                 {  
  64.                     high = mid - 1;  
  65.                 }  
  66.             }  
  67.   
  68.             return -1;  
  69.         }  
  70.     }  
  71. }  
  72.   
  73. // Output:  
  74. /* 
  75. */  
  76. 時間複雜度:O(log2n)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.