📜  Scala中的二进制搜索

📅  最后修改于: 2022-05-13 01:55:11.861000             🧑  作者: Mango

Scala中的二进制搜索

Binary Search 是一种算法,可以帮助我们在 O(log n) 时间内找到排序数组中的元素。它的算法工作在分而治之的原则上,只有在对可用数据进行排序时才有效。
现在,当我们使用二分查找时,会出现三种情况:

  1. 如果中间元素是要搜索的元素,我们返回中间元素的索引。
  2. 如果中间元素小于要查找的元素,我们以同样的方式在右边的子数组(Mid to End)中查找(获取新的中间元素并再次检查所有三种情况)。我们在右子数组中搜索,因为数据已排序,因此中间元素之前的所有元素也将小于或等于中间元素。
  3. 如果中间元素大于要搜索的元素,我们在左边的子数组中搜索(Start to Mid)。

这个过程一直持续到我们找到要搜索的元素或者子数组的大小减小到零。

例子:
Scala中的二进制搜索

在 Scala 中执行二分搜索有三种方法。

递归方法

在递归方法中,我们递归地调用具有更新的 start 和 end 值的二进制搜索算法,直到我们将中间元素与要搜索的元素匹配或数组大小减小到零。下面是 Scala 中二分搜索的递归方法的代码。

// Scala code for Recursive Binary Search
  
// Creating object
object GFG{
  
// Creating a recursive  Binary Search function
def RecursiveBinarySearch(arr: Array[Int],
                          Element_to_Search: Int)
                         (low: Int = 0,
                          high: Int = arr.length - 1): Int = 
{
      
    // If element not found                               
    if (low > high) 
        return -1
      
    // Getting the middle element
    var middle = low + (high - low) / 2
      
    // If element found
    if (arr(middle) == Element_to_Search)
        return middle
      
    // Searching in the left half
    else if (arr(middle) > Element_to_Search)
        return RecursiveBinarySearch(arr, 
               Element_to_Search)(low, middle - 1)
      
    // Searching in the right half
    else
        return RecursiveBinarySearch(arr, 
               Element_to_Search)(middle + 1, high)
}
  
// Creating main function
def main(args: Array[String]){
      
    // Calling the binary search function and
    // storing its result in index variable
    var index = RecursiveBinarySearch(Array(1, 2, 3, 4, 55, 
                                            65, 75), 4)(0, 6);
      
    // If value not found 
    if(index == -1)
       print("Not Found")
          
    // Else print the index where 
    // the value is found
    else
       print("Element found at Index " + index)
}
}

输出

Element found at Index 3

迭代方法

在迭代方法中,我们运行一个 while 循环,直到我们找到要搜索的元素或数组大小减小到零。下面是 Scala 中二进制搜索的迭代方法的代码。

// Scala code for Iterative Binary Search
  
// Creating object
object GFG{
  
// Creating Binary Search function
// Accepting the passed array and 
// element to be searched
def IterativeBinarySearch(arr: Array[Int], 
                          Element_to_Search: Int): Int =
{
      
    // Creating start variable to
    // point to the first value
    var low = 0
      
    // Creating end variable to 
    // point to the last value
    var high = arr.length - 1
      
    // Finding the value in the 
    // array iteratively
    while (low <= high)
    {
          
        // Getting middle element    
        var middle = low + (high - low) / 2
          
        // If element found in the middle index
        if (arr(middle) == Element_to_Search)
            return middle
          
        // Searching in the first half
        else if (arr(middle) > Element_to_Search)
            high = middle - 1
          
        // Searching in the second half  
        else
            low = middle + 1
    }
      
    // If value not found in the 
    // entire array , return -1 
    -1
}
  
// Creating main function
def main(args: Array[String])
{
      
    // Calling the binary search function and
    // storing its result in index variable
    var index = IterativeBinarySearch(Array(1, 2, 3, 4, 55,
                                            65, 75), 65);
      
    // If value not found 
    if(index == -1)
       print("Not Found")
          
    // Else print the index where 
    // the value is found
    else
       print("Element found at Index " + index)
}
}

<输出

Element found at Index 5

模式匹配和函数式编程方法

在此,我们首先将中间元素与要搜索的元素进行匹配。如果元素存在,我们返回它的索引。否则,我们继续使用更新的参数调用创建的函数。以下是该方法的代码:

// Scala code for Iterative Binary Search
  
// Creating object
object GFG{
      
// Using the functional programming approach
def FunctionalBinarySearch(arr: Array[Int], 
                           Element_to_Search: Int): Int =
{ 
    def BinarySearch(arr: Array[Int],
                     Element_to_Search: Int, 
                     low: Int, high: Int): Int =
    {
              
        // If element not found
        if (low > high)
            return -1
              
        // Getting middle index
        var middle = low + (high - low) / 2
              
        // Pattern matching
        arr match
        {
                  
            // If element found , return the index
            case(arr:Array[Int]) if (arr(middle) ==
                                     Element_to_Search) => 
                                     middle 
                  
            // Call the function for the second half
            case(arr:Array[Int]) if (arr(middle) < 
                                     Element_to_Search) => 
                                     BinarySearch(arr, 
                                     Element_to_Search,
                                     middle + 1, high)
                  
            // Call the function for the first half 
            case(arr:Array[Int]) if (arr(middle) > 
                                     Element_to_Search) => 
                                     BinarySearch(arr, 
                                     Element_to_Search,
                                     low, middle - 1)
        }
    } 
          
    // Calling the Binary Search function
    BinarySearch(arr, Element_to_Search, 0, arr.length - 1)
}
      
// Creating main function
def main(args: Array[String]){
  
    // Calling the binary search function and 
    // storing its result in index variable
    var index = FunctionalBinarySearch(Array(1, 2, 3, 4, 55,
                                             65, 75), 44);
  
    // If value not found 
    if(index == -1)
    print("Element not found")
          
    // Else print the index where 
    // the value is found
    else
    print("Element found at Index " + index)
}
}

输出

Element not found