Search structure (static)

  • Searching is to find data elements that meet certain conditions in the data collection. The most common way is to give a value in advance and find the element whose key code is equal to the given value in the set. The results of the search usually have two possibilities: one possibility is that the search is successful, that is, data elements that meet the conditions are found. At this time, as a result, the position of the element in the structure can be reported, and the specific information in the element can be further given. The latter is called retrieval in the database. Another possibility is that the search was unsuccessful or the search failed. As a result, some information should also be reported, such as failure signs, failure locations, etc.
  • The data set used for searching is usually called the search structure , which is composed of elements (or records) of the same data type. In the search structure, each element (or record) is called an object, and the search structure can be defined by object classes.
  • There are several attributes in each object, of which there should be one attribute whose value can uniquely identify this object. He called it a key code. Using key code-based retrieval, the search result should be unique. However, in practical applications, the search conditions are various. For example, search by book title and search by author should be allowed in the library... In this way, attribute-based search methods can be used, but the search results may not be unique.
  • Static search is the search in our usual concept, which is "real search". The reason why static search is a real search, because in the process of static search is only a "search" operation , namely: (1) Check whether a specific keyword is in the table (judgmental search); (2) Search Various attributes of a specific keyword data element (retrievable search). These two operations are only to obtain data information in an existing table, without any changes to the data elements and structure of the table, which is the so-called static search.
  • Dynamic search is not like "finding", but more like a process of "creating, expanding, modifying, and deleting" a table . There will be two more actions on the table in the dynamic search process: (1) First, there is also a "judgmental search" process. If a certain keyword does not exist in the table, it will be inserted into the table according to certain rules. Medium; (2) If it already exists, you can delete it. Although the dynamic search process only adds "insert" and "delete" operations, it is often not so simple when performing these two operations on a specific table.

Sequential search

Sequential search is one of the most basic search methods. Sequential search is also called linear search, which is mainly used to search in linear tables. ,

int ordersearch(int a[], int n, int des){
    
    
  int i;
  for(i=0; i<n; i++){
    
    
   if(des==a[i])
     return 1;
  }
 return 0;
}

Sequence search and binary search based on ordered sequence table

//顺序搜索
int ordersearch(int a[], int n, int des){
    
    
  int i;
  for(i=0; i<n; i++){
    
    
   if(des==a[i])
     return 1;
   else if(des<a[i]) break;
  }
 return 0;
}
//折半搜索非递归实现
public int bsearch(int[] a, int n, int value) {
    
      
    int low = 0;  int high = n - 1;  
    while (low <= high) {
    
        
       int mid = (low + high) / 2;    
       if (a[mid] == value) {
    
          
         return mid;
       } else if (a[mid] < value) {
    
          
          low = mid + 1;    
       } else {
    
          
          high = mid - 1;    
       }  
   }  
   return -1; 
}
// 二分查找的递归实现 
public int bsearch(int[] a, int n, int val) {
    
      
    return bsearchInternally(a, 0, n - 1, val);
} 
private int bsearchInternally(int[] a, int low, int high, int value) {
    
      
    if (low > high) return -1;  
    int mid =  low + ((high - low) >> 1);  
    if (a[mid] == value) {
    
        
       return mid;  
    } else if (a[mid] < value) {
    
        
       return bsearchInternally(a, mid+1, high, value);  
    } else {
    
        
       return bsearchInternally(a, low, mid-1, value);  
    } 
}

Fibonacci search

If an ordered list with n elements, n=F(k)-1, which is 1 less than a certain Fibonacci number. If n is not exactly equal to a certain Fibonacci number, some virtual elements can be added , Make it reach a certain Fibonacci number. For example, n=F(7)-1=12, low=1, hight=n=12, mid=F(6)=8 can be taken.

public static int fibonacciSearch(int[] a, int n, int key) {
    
    
          int low = 0;
          int high = n - 1;
  
          int F[] = new int[max_size];
          fibonacci(F);//构建斐波那契数组
  
          //计算 n 位于斐波那契数列的位置
          int k = 0;
          while (n > F[k] - 1)
             ++k;
 
         //将数组a扩展到F[k]-1的长度,即增加了虚元素
          int tmp[];
          tmp = new int[F[k] - 1];
          System.arraycopy(a, 0, tmp, 0, n);
 
          for (int i = n; i < F[k] - 1; ++i)
              tmp[i] = a[n - 1];//用数组最后一个元素扩展
 
          while (low <= high) {
    
    
              int mid = low + F[k - 1] - 1;//借助斐波纳契数确定位置,减1是low从0开始
              if (key < tmp[mid]) {
    
    
                  high = mid - 1;
                  k -= 1;
              } else if (tmp[mid] < key) {
    
    
                  low = mid + 1;
                  k -= 2;
              } else {
    
    
                  if (mid < n)
                      return mid;
                  else
                      return n - 1;
              }
          }
          tmp = null;
          return -1;
      }
  • When n is very large (n>10), this search method is called the golden section method, and its average performance is better than half search. But in the worst case, the performance is worse than the binary search. The average search length for a successful search is also O(log2n). The advantage of this method is to find the midpoint mid without doing division, just do addition and subtraction.

Guess you like

Origin blog.csdn.net/qq_36828822/article/details/104109735