# 15 day fast algorithm series -- the fourth day five classic search [1]

From: http://blog.csdn.net/m13666368773/article/details/7516433

In our life, there is no place where there is no search. For example, find out which mm is the most pl at work, guess the age of MM... All these are search.

In our algorithm, one is called linear search.

Divided into: sequential search.

Half search.

There are two forms of searching:

Divided into: destructive search, such as a group of mm, I guess their age, the first guess is 23 +, at this time this mm has been remove d from the mmlist in my mind.

Brother doesn't find 23 +, so this kind of search destroys the original structure.

Non destructive search, on the contrary, does not destroy the structure.

Order lookup:

This is very simple, that is, after an array, one by one ratio, until we find it.

1. using System;
2.  using System.Collections.Generic;
3.  using System.Linq;
4.  using System.Text;
5.
6.  namespace Sequential
7.  {
8.      class Program
9.      {
10.          static void Main(string[] args)
11.          {
12.              List<int> list = new List<int>() { 2, 3, 5, 8, 7 };
13.
14.              var result = SequenceSearch(list, 3);
15.
16.              if (result != -1)
17.                  Console.WriteLine("3 Found in array, index position:" + result);
18.              else
19.                  Console.WriteLine("Wuwu, I can't find it!");
20.
22.          }
23.
24.          //Sequential search
25.          static int SequenceSearch(List<int> list, int key)
26.          {
27.              for (int i = 0; i < list.Count; i++)
28.              {
29.                  //Search succeeded, return serial number
30.                  if (key == list[i])
31.                      return i;
32.              }
33.              //Failed to find, return - 1
34.              return -1;
35.          }
36.      }
37.  }

Half search: this kind of search is very interesting, that is to cut off half every time,

For example, in the "lucky 52" price guessing game, the price is below 999 yuan. You can guess how many to give in one minute. If those players know how to search in half,

That's quite the result.

Note, however, that there are two disadvantages to this search:

First: arrays must be ordered. If they are not, they must be ordered. You know that the fastest sorting is NLogN, so.

Second, this search is limited to linear sequential storage structures.

Upper Code:

1. using System;
2.  using System.Collections.Generic;
3.  using System.Linq;
4.  using System.Text;
5.
6.  namespace BinarySearch
7.  {
8.      class Program
9.      {
10.          static void Main(string[] args)
11.          {
12.              List<int> list = new List<int>() { 3, 7, 9, 10, 11, 24, 45, 66, 77 };
13.
14.              var result = BinarySearch(list, 45);
15.
16.              if (result != -1)
17.                  Console.WriteLine("45 Found in array, index position:" + result);
18.              else
19.                  Console.WriteLine("Wuwu, I can't find it!");
20.
22.          }
23.
24.          ///<summary>
25.  ///Half search
26.  ///</summary>
27.  ///<param name="list"></param>
28.  ///<returns></returns>
29.          public static int BinarySearch(List<int> list, int key)
30.          {
31.              //Lowest line
32.              int low = 0;
33.
34.              //top-line
35.              int high = list.Count - 1;
36.
37.              while (low <= high)
38.              {
39.                  //Take the middle value
40.                  var middle = (low + high) / 2;
41.
42.                  if (list[middle] == key)
43.                  {
44.                      return middle;
45.                  }
46.                  else
47.                      if (list[middle] > key)
48.                      {
49.                          //Half down
50.                          high = middle - 1;
51.                      }
52.                      else
53.                      {
54.                          //Half way up
55.                          low = middle + 1;
56.                      }
57.              }
59.              return -1;
60.          }
61.      }
62.  }

As we have said before, there is a form of searching that is destructive, so it is very sad for the data of linear structure, because every time it is broken,

It may cause the whole array element to move forward or backward.

So the search of linear structure is not suitable for destructive operation. Is there any other way to solve it? Well, there must be, but I'll share it the next day.

ps: linear search time complexity: O(n);

The time complexity of half disorder (with fast live stacking): O(NlogN)+O(logN);

Half ordered time complexity: O(logN);

Posted by vombomin on Fri, 01 May 2020 06:23:34 -0700