Algorithms Series 15 Days Quick - Fifth Date Five Classic Search

Posted by atl_andy on Wed, 15 May 2019 18:49:17 +0200

Algorithms Series 15 Days Quick - Fifth Date Five Classic Search


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

Do you know that in fact, there is an O(1) search, the so-called second kill.

 

Hash Search:

 

Yeah, he's hash lookup. When it comes to hash, you must mention hash functions. Ha-ha, this thing has already formed in our minds.

Inherent thinking. You must know the corresponding relationship in Hash.

For example, "5" is a number to be saved, and then I throw it to the hash function, which returns me a "2", then the "5" at this time.“

And "2" to establish a corresponding relationship, this relationship is the so-called "hash relationship", in practical applications also formed "2" is key, "5" is value.

Then some friends will ask how to do hashing. First of all, to do hashing must abide by two principles:

(1) Keys are as dispersed as possible, that is, I throw a "6" and "5" to you, and you return a "2", so the hash function is not perfect.

(2) Hash function is as simple as possible, that is to say, throw a "6" to you, and your hash function will take an hour to give me, which is not good.

 

In fact, there are five ways to do hash:

The first one is "direct address method".

It's easy to understand that key=Value+C; this "C" is a constant. Value+C is actually a simple hash function.

The second one is "dividing to get the remainder".

It's easy to understand, key=value%C; the explanation is the same as above.

The third is "digital analysis".

This is interesting, for example, a set of values 1 = 112233, 2 = 112633, and 3 = 119033.

For such numbers, we analyze the fluctuation of the two numbers in the middle, while the others remain unchanged. So we can take the value of the key.

                  key1=22,key2=26,key3=90.  

The fourth one is "the square is in the middle". Ignore here. See fame.

The fifth is folding method.

This is interesting, for example, value=135790, requiring key to be a hash value of two digits. Then we change value to 13 + 57 + 90 = 160.

Then remove the high "1" and then key=60, haha, that's their hash relationship. The purpose of doing this is that key and every value are alike.

Close, to achieve the "hash address" as scattered as possible.

 

So-called often walking by the river, there are no wet shoes. Hash is the same. The design of your hash function is so good that it will hit the building one time or another. So the question thrown to us is

That is, if we resolve the hash address conflict.

 

In fact, there are also two common ways to resolve conflicts:

 

The first is "Open Address Method".

The so-called "open address" is actually an unused address in an array. That is to say, where a conflict occurs, the element that comes next (in two ways)

Linear detection and function detection) Look for the "open address" after the array and insert themselves into it.

 

The second is "link method".

It doesn't matter if you don't understand this for a while, so I'll introduce the principle of putting a pointer field on each element, where conflicts occur, and then the one that follows.

When an element throws its own data domain to the element in conflict, a list is formed where the conflict occurs.

 

There is so much verbosity above, that is to say, I want you to have some reference and means in the two aspects of "designing hash" and "resolving conflict".

 

So here's the code.

The design function adopts "dividing and residual method".

In the aspect of conflict, the method of "open address linear detection" is adopted.

  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Linq;  
  4.  using System.Text;  
  5.    
  6.  namespace HashSearch  
  7.  {  
  8.      class Program  
  9.      {  
  10.          //"Division and Remainder"  
  11.          static int hashLength = 13;  
  12.    
  13.          //Original data  
  14.          static List<int> list = new List<int>() { 13, 29, 27, 28, 26, 30, 38 };  
  15.    
  16.          //Hash table length  
  17.          static int[] hash = new int[hashLength];  
  18.    
  19.          static void Main(string[] args)  
  20.          {  
  21.              //Create hash  
  22.              for (int i = 0; i < list.Count; i++)  
  23.              {  
  24.                  InsertHash(hash, hashLength, list[i]);  
  25.              }  
  26.    
  27.              Console.WriteLine("Hash Data:" + string.Join(",", hash));  
  28.    
  29.              while (true)  
  30.              {  
  31.                  Console.WriteLine("\n Please enter the number you want to find:");  
  32.                  int result = int.Parse(Console.ReadLine());  
  33.                  var index = SearchHash(hash, hashLength, result);  
  34.    
  35.                  if (index != -1)  
  36.                      Console.WriteLine("number" + result + "The location of the index is:" + index);  
  37.                  else  
  38.                      Console.WriteLine("Whining," + result + " stay hash No one found it!");  
  39.    
  40.              }  
  41.          }  
  42.    
  43.          ///<summary>  
  44.  /// Hash table to retrieve data  
  45.  ///</summary>  
  46.  ///<param name="dic"></param>  
  47.  ///<param name="hashLength"></param>  
  48.  ///<param name="key"></param>  
  49.  ///<returns></returns>  
  50.          static int SearchHash(int[] hash, int hashLength, int key)  
  51.          {  
  52.              //hash function  
  53.              int hashAddress = key % hashLength;  
  54.    
  55.              //To specify that the hash Adrress corresponding value exists but is not the key value, the open addressing method is used to solve the problem.  
  56.              while (hash[hashAddress] != 0 && hash[hashAddress] != key)  
  57.              {  
  58.                  hashAddress = (++hashAddress) % hashLength;  
  59.              }  
  60.    
  61.              //Finding an open cell indicates a search failure  
  62.              if (hash[hashAddress] == 0)  
  63.                  return -1;  
  64.              return hashAddress;  
  65.    
  66.          }  
  67.    
  68.          ///<summary>  
  69.  /// Data insertion into Hash table  
  70.  ///</summary>  
  71.  /// <param-name="dic">hash table</param>  
  72.  ///<param name="hashLength"></param>  
  73.  ///<param name="data"></param>  
  74.          static void InsertHash(int[] hash, int hashLength, int data)  
  75.          {  
  76.              //hash function  
  77.              int hashAddress = data % 13;  
  78.    
  79.              //If the key exists, it means that it has been occupied by others, and the conflict must be resolved at this time.  
  80.              while (hash[hashAddress] != 0)  
  81.              {  
  82.                  //Find it by open addressing  
  83.                  hashAddress = (++hashAddress) % hashLength;  
  84.              }  
  85.    
  86.              //Store data in a dictionary  
  87.              hash[hashAddress] = data;  
  88.          }  
  89.      }  
  90.  }  

Result:

 

 

Index lookup:

When referring to "index", we estimate that the first reaction is "database index". Right, in fact, the primary key to establish "index" is to facilitate our search in massive data.

As for the knowledge of indexing, it is estimated that everyone knows better than I do. I'll give you a brief introduction.

We write our own algorithms to implement the three terms commonly used in index lookup:

First: main table, this is very simple, to find the object.

Second: Index items. Generally, we use functions to divide a main table into several sub-tables. Each sub-table establishes an index. This index is called index items.

Third: Index table, the collection of index items is index table.

 

Generally, "index item" contains three contents: index, start, length.

First: index, which is the key word that the index points to the main table.

Second: start, which is the location of index in the main table.

Third: length, which is the interval length of a subtable.

  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Linq;  
  4.  using System.Text;  
  5.    
  6.  namespace IndexSearchProgram  
  7.  {  
  8.      class Program  
  9.      {  
  10.          ///<summary>  
  11.  /// Index Item Entities  
  12.  ///</summary>  
  13.          class IndexItem  
  14.          {  
  15.              //Values corresponding to the main table  
  16.              public int index;  
  17.              //Starting position of main table record interval  
  18.              public int start;  
  19.              //Length of main table record interval  
  20.              public int length;  
  21.          }  
  22.    
  23.          static void Main(string[] args)  
  24.          {  
  25.              Console.WriteLine("The original data are as follows:" + string.Join(",", students));  
  26.    
  27.    
  28.              int value = 205;  
  29.    
  30.              Console.WriteLine("\n insert data" + value);  
  31.    
  32.              //Insert 205 into a collection, overindex  
  33.              var index = insert(value);  
  34.    
  35.              //If the insertion is successful, get the location of 205 elements  
  36.              if (index == 1)  
  37.              {  
  38.                  Console.WriteLine("\n Data after insertion:" + string.Join(",", students));  
  39.                  Console.WriteLine("\n Data element: 205 is located in the array " + indexSearch(205) + "position");  
  40.              }  
  41.    
  42.              Console.ReadLine();  
  43.          }  
  44.    
  45.          ///<summary>  
  46.  /// Student master list  
  47.  ///</summary>  
  48.          static int[] students = {   
  49.                                     101,102,103,104,105,0,0,0,0,0,  
  50.                                     201,202,203,204,0,0,0,0,0,0,  
  51.                                     301,302,303,0,0,0,0,0,0,0  
  52.                                  };  
  53.          ///<summary>  
  54.  /// Student Index Table  
  55.  ///</summary>  
  56.          static IndexItem[] indexItem = {   
  57.                                    new IndexItem(){ index=1, start=0, length=5},  
  58.                                    new IndexItem(){ index=2, start=10, length=4},  
  59.                                    new IndexItem(){ index=3, start=20, length=3},  
  60.                                  };  
  61.    
  62.          ///<summary>  
  63.  /// Find data  
  64.  ///</summary>  
  65.  ///<param name="key"></param>  
  66.  ///<returns></returns>  
  67.          public static int indexSearch(int key)  
  68.          {  
  69.              IndexItem item = null;  
  70.    
  71.              //Establishment of indexing rules  
  72.              var index = key / 100;  
  73.    
  74.              //First go to the index.  
  75.              for (int i = 0; i < indexItem.Count(); i++)  
  76.              {  
  77.                  if (indexItem[i].index == index)  
  78.                  {  
  79.                      item = new IndexItem() { start = indexItem[i].start, length = indexItem[i].length };  
  80.                      break;  
  81.                  }  
  82.              }  
  83.    
  84.              //If item is null, the search fails in the index  
  85.              if (item == null)  
  86.                  return -1;  
  87.    
  88.              for (int i = item.start; i < item.start + item.length; i++)  
  89.              {  
  90.                  if (students[i] == key)  
  91.                  {  
  92.                      return i;  
  93.                  }  
  94.              }  
  95.              return -1;  
  96.          }  
  97.    
  98.          ///<summary>  
  99.  /// Insert data  
  100.  ///</summary>  
  101.  ///<param name="key"></param>  
  102.  ///<returns></returns>  
  103.          public static int insert(int key)  
  104.          {  
  105.              IndexItem item = null;  
  106.              //Indexing rules  
  107.              var index = key / 100;  
  108.              int i = 0;  
  109.              for (i = 0; i < indexItem.Count(); i++)  
  110.              {  
  111.                  //Get the index  
  112.                  if (indexItem[i].index == index)  
  113.                  {  
  114.                      item = new IndexItem()  
  115.                      {  
  116.                          start = indexItem[i].start,  
  117.                          length = indexItem[i].length  
  118.                      };  
  119.                      break;  
  120.                  }  
  121.              }  
  122.              if (item == null)  
  123.                  return -1;  
  124.              //Update master table  
  125.              students[item.start + item.length] = key;  
  126.              //Update index table  
  127.              indexItem[i].length++;  
  128.              return 1;  
  129.          }  
  130.      }  
  131.  }  

  

Result:

 

ps: Hash lookup time complexity O(1).

Index lookup time complexity: In the case of Demo above, it is equal to O(n/3)+O(length)


Topics: Database