# Swift Program to Implement Binary Search Algorithm

In swift, Binary search algorithm is used to search element from the sorted array. It repeatedly divides the search interval into half and then search the specified element. In the binary search algorithm, the input array must be in sorted order to reduce the time complexity.

## Algorithm

• Step 1 − Create a function to implement a binary search algorithm.

• Step 2 − Inside the function, first we find the lower bound and upper bound range of the given array.

• Step 3 − Run a while loop till LBound<=UBound.

• Step 4 − Now find the middle value of the given range. And check if the middle value is equal to the key element. If yes, then return the index value.

• Step 5 − Check if the key element is less than a middle index, then search the upper half range.

• Step 6 − If the key element is greater than the middle index, then search the lower half range.

• Step 7 − If the key element is not found in the given array, then return nil.

• Step 8 − Outside the function create an array.

• Step 9 − Call the function and pass the array and the key element into it.

• Step 10 − Print the index of the searched element.

## Example

In the following example, we will create a function named as binarySearchAlgo(). This function takes a sorted array as input and finds if the given array contains the specified array or not. So this function has two variables named as LBound and UBound to keep track of the range of the array for the search. Then we run a while loop until LBound<=UBound. Inside the loop, we find the middle index of the given range and then check for the following conditions −

If the key element is equal to the middle element, then return the middle index.

If the middle element is less than the key element, then search the upper half range.

If the middle element is greater than the key element, then search the lower half range.

If the key element is not found in the given array, then return nil.

import Foundation
import Glibc

// Function to search an array element using binary search algorithm
func binarySearchAlgo(arr: [Int], key: Int) -> Int? {

var LBound = 0
var UBound = arr.count - 1

while LBound <= UBound {
let middle = (LBound + UBound) / 2
if arr[middle] == key {

// Found the key element
return middle
} else if arr[middle] < key {

// Searching in the upper half
LBound = middle + 1
} else {

// Searching in the lower half
UBound = middle - 1
}
}

return nil
}

let arr = [4, 6, 8, 24, 67, 90]
if let indexValue = binarySearchAlgo(arr: arr, key: 24) {
print("Found at index: \(indexValue)")
} else {
}


## Output

Found at index: 3


## Conclusion

So this is how we can implement a binary search algorithm. The binary search is the fastest searching algorithm because the input array is sorted. In this article, we use an iterative method to implement a binary search algorithm whose time complexity is O(log n). The binary search algorithm works pretty well for small as well as larger arrays. The major drawback of binary search algorithms is that it requires a sorted array. If the array is not sorted, then it sorts the array which adds extra O(nlogn) time complexity.

Updated on: 13-Apr-2023

567 Views 