📜  统一二元搜索

📅  最后修改于: 2021-04-24 15:27:19             🧑  作者: Mango

当在同一数组或相同大小的多个数组上进行多次搜索时,统一二进制搜索是对二进制搜索算法的优化。在普通的二进制搜索中,我们进行算术运算以找到中点。在这里,我们预先计算中点并将其填充到查找表中。数组查找通常比算术完成(加法和移位)查找中点的速度更快。

例子:

Input : array={1, 3, 5, 6, 7, 8, 9}, v=3
Output : Position of 3 in array = 2

Input :array={1, 3, 5, 6, 7, 8, 9}, v=7
Output :Position of 7 in array = 5

该算法与二进制搜索算法非常相似,唯一的区别是为数组创建了一个查找表,该查找表用于修改数组中指针的索引,从而使搜索更快。该算法不维护上下限,而是维护索引,并使用查找表修改索引。

C++
// C++ implemenatation of above approach
#include 
using namespace std;
  
const int MAX_SIZE = 1000;
  
// lookup table
int lookup_table[MAX_SIZE];
  
// create the lookup table
// for an array of length n
void create_table(int n)
{
    // power and count variable
    int pow = 1;
    int co = 0;
    do {
        // multiply by 2
        pow <<= 1;
  
        // initialize the lookup table
        lookup_table[co] = (n + (pow >> 1)) / pow;
    } while (lookup_table[co++] != 0);
}
  
// binary search
int binary(int arr[], int v)
{
    // mid point of the array
    int index = lookup_table[0] - 1;
  
    // count
    int co = 0;
  
    while (lookup_table[co] != 0) {
  
        // if the value is found
        if (v == arr[index])
            return index;
  
        // if value is less than the mid value
        else if (v < arr[index])
            index -= lookup_table[++co];
  
        // if value is greater than the mid value
        else
            index += lookup_table[++co];
    }
}
  
// main function
int main()
{
  
    int arr[] = { 1, 3, 5, 6, 7, 8, 9 };
    int n = sizeof(arr) / sizeof(int);
  
    // create the lookup table
    create_table(n);
  
    // print the position of the array
    cout << "Position of 3 in array = " 
         << binary(arr, 3) << endl;
  
    return 0;
}


Java
// Java implemenatation of above approach 
class GFG
{
      
    static int MAX_SIZE = 1000; 
      
    // lookup table 
    static int lookup_table[] = new int[MAX_SIZE]; 
      
    // create the lookup table 
    // for an array of length n 
    static void create_table(int n) 
    { 
        // power and count variable 
        int pow = 1; 
        int co = 0; 
        do 
        { 
            // multiply by 2 
            pow <<= 1; 
      
            // initialize the lookup table 
            lookup_table[co] = (n + (pow >> 1)) / pow; 
        } while (lookup_table[co++] != 0); 
    } 
      
    // binary search 
    static int binary(int arr[], int v) 
    { 
        // mid point of the array 
        int index = lookup_table[0] - 1; 
      
        // count 
        int co = 0; 
      
        while (lookup_table[co] != 0) 
        { 
      
            // if the value is found 
            if (v == arr[index]) 
                return index; 
      
            // if value is less than the mid value 
            else if (v < arr[index]) 
            {
                index -= lookup_table[++co]; 
                return index;
            }
              
            // if value is greater than the mid value 
            else
            {
                index += lookup_table[++co];
                return index;
            }
        } 
        return index ;
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
      
        int arr[] = { 1, 3, 5, 6, 7, 8, 9 }; 
        int n = arr.length; 
      
        // create the lookup table 
        create_table(n); 
      
        // print the position of the array 
        System.out.println( "Position of 3 in array = " + 
                                    binary(arr, 3)) ;
      
      
    } 
}
  
// This code is contributed by Ryuga


Python3
# Python3 implemenatation of above approach
  
MAX_SIZE = 1000
  
# lookup table
lookup_table = [0] * MAX_SIZE
  
# create the lookup table
# for an array of length n
def create_table(n):
      
    # power and count variable
    pow = 1
    co = 0
    while True:
          
        # multiply by 2
        pow <<= 1
  
        # initialize the lookup table
        lookup_table[co] = (n + (pow >> 1)) // pow
        if lookup_table[co] == 0:
            break
        co += 1
  
# binary search
def binary(arr, v):
      
    # mid point of the array
    index = lookup_table[0] - 1
  
    # count
    co = 0
  
    while lookup_table[co] != 0:
  
        # if the value is found
        if v == arr[index]:
            return index
  
        # if value is less than the mid value
        elif v < arr[index]:
            co += 1
            index -= lookup_table[co]
  
        # if value is greater than the mid value
        else:
            co += 1
            index += lookup_table[co]
  
# main function
arr = [1, 3, 5, 6, 7, 8, 9]
n = len(arr)
  
# create the lookup table
create_table(n)
  
# print the position of the array
print("Position of 3 in array = ", binary(arr, 3))
  
# This code is contributed by divyamohan123


C#
// C# implemenatation of above approach 
using System;
      
class GFG
{
      
    static int MAX_SIZE = 1000; 
      
    // lookup table 
    static int []lookup_table = new int[MAX_SIZE]; 
      
    // create the lookup table 
    // for an array of length n 
    static void create_table(int n) 
    { 
        // power and count variable 
        int pow = 1; 
        int co = 0; 
        do
        { 
            // multiply by 2 
            pow <<= 1; 
      
            // initialize the lookup table 
            lookup_table[co] = (n + (pow >> 1)) / pow; 
        } while (lookup_table[co++] != 0); 
    } 
      
    // binary search 
    static int binary(int []arr, int v) 
    { 
        // mid point of the array 
        int index = lookup_table[0] - 1; 
      
        // count 
        int co = 0; 
      
        while (lookup_table[co] != 0) 
        { 
      
            // if the value is found 
            if (v == arr[index]) 
                return index; 
      
            // if value is less than the mid value 
            else if (v < arr[index]) 
            {
                index -= lookup_table[++co]; 
                return index;
            }
              
            // if value is greater than the mid value 
            else
            {
                index += lookup_table[++co];
                return index;
            }
        } 
        return index ;
    } 
      
    // Driver code 
    public static void Main () 
    { 
      
        int []arr = { 1, 3, 5, 6, 7, 8, 9 }; 
        int n = arr.GetLength(0); 
      
        // create the lookup table 
        create_table(n); 
      
        // print the position of the array 
    Console.WriteLine( "Position of 3 in array = " + 
                                    binary(arr, 3)) ;
      
      
    } 
}
  
/* This code contributed by PrinciRaj1992 */


输出:
Position of 3 in array = 1

参考资料: https : //en.wikipedia.org/wiki/Uniform_binary_search