Home php教程 PHP开发 Introduction to binary search

Introduction to binary search

Dec 19, 2016 pm 04:29 PM

Binary search

Today we will talk about "binary search". The idea of ​​binary search is to compare the size with the middle number of a certain array in a sequential array each time. The disadvantage of binary search is that the array must be sequential (I take the data sorted from small to large as an example). The advantage is that the query efficiency is extremely high and the time complexity is log2n. The more this search method is used in big data, the more obvious the effect will be. The source code and unit test are attached below. The source code contains two algorithms, one is loop and the other is recursive. Please refer to it:
Source code:

                                                                                                                                                                                                                                     . (Recursive method)//// The array must be sorted from small to large. If it is unavailable data, it can be used & lt; see cref = "bitmapsort"/& gt; >Classes are sorted.
                     /// If the value to be found does not exist in the array, return -1.
                                                                                                          param>
                                                                                                                                                                ,,,,,,,,,,,,,,,,,,, if (array == null) { throw new ArgumentException("array is null"); }
            if (array[array.Length - 1] == value) { return array.Length - 1; }
                                                                                                 0, array.Length - 1, value);
                                                                   private static int Search(int[] array, int beginIndex, int endIndex, int value)                                                                          if ( endIndex == beginIndex)
                                                                                                                                                       == value) { return beginIndex; }
                                                                                                                                                                                                                                                                                    [middle] > value)
                                                                                                                                                               ​return -1;
                                                                                             (Loop method)
          /// The array must be sorted from small to large. If it is unsorted data, you can use the class
                                      or                                                >Classes are sorted.
                     /// If the value to be found does not exist in the array, return -1.
                                                                                                          Param & gt;
/// & lt; Return & gt; return to the first few, if the value you want to find does not exist in the array, return to -1 & lt;/return;
{
int beginIndex = 0;
int endIndex = array.Length - 1;
while (true)
if (endIndex - Index <= 1)
begin                                                                                                                                                                                                                                     if (value == array[beginIndex]) { return beginIndex; }
                                                                                                                                         endIndex) / 2;
               if (value < array[middle]) { endIndex = middle; }
               if (value == array[middle]) { return middle; }
               if (value > array[middle]) { beginIndex = middle; }
           }
       }

单元测试:
       [TestMethod()]
       [DeploymentItem("ZjyWorkCodeLibrary.dll")]
       public void SearchTest()
       {
           Random random = new Random();
           int[] array2 = new int[100];
           for (int i = 0; i < 100; i++)
           {
               array2[i] = random.Next(0, 1000);
           }
           int[] array3 = BitmapSort.Sort(array2);
           for (int i = 0; i < array3.Length; i++)
           {
               Assert.AreEqual(BinarySearch.Search(array3, array3[i]), i);
               Assert.AreEqual(BinarySearch.Search2(array3, array3[i]), i);
           }
           Assert.AreEqual(BinarySearch.Search(array3, 9999), -1);
           Assert.AreEqual(BinarySearch.Search2(array3, 9999), -1);
       }



更多二分法查找介绍相关文章请关注PHP中文网!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)