First Occurrence Using Binary Search in Python

In this tutorial, we will see how to search for the first occurrence of an element in the given sorted list using Binary Search. We will implement the algorithm in Python. But first, we need to know what Binary Search is.

Naïve Approach

Before beginning with the binary search, let us first see the simplest solution to the problem.

We will run a for loop, and when we find x, we will break the loop and return the current index of the variable. We can simultaneously get the index of the variable using the enumerate function of Python.

Code

Output:

The first occurrence of 3 in the array is at: 
7

The time complexity of this approach is O(n), where n is the number of elements in the array. This method is not very efficient. We can consider this method when we are not provided with a sorted array.

Another way to solve this problem is by using the Python list's .index(e) function. This function will return the array index containing the first occurrence of the given element e.

Code

Output:

The first occurrence of 3 in the array is at: 
7

Binary Search

For instance, we have a sorted array or a sorted Python list of n integer numbers. We want to search for a specific number in the list. Using for loop and traversing through each element of the list and checking if the current element is equal to the number we are looking for has a time complexity of O(n) where n is the number of integers in the list. Binary search Is an efficient way to solve this task.

The algorithm of binary is as follows:

  • We will divide the given sorted list into two halves.
  • Check if the target number equals the element present in the middle of the list. If yes, then return the index of the middle element.
  • If no, check if the target number is greater or lesser than the element in the middle of the list.
  • If the target number exceeds the middle element, we will discard the left half of the list. If the target number is less than the middle element of the list, we will discard the right half of the list.
  • After the previous step, we will have a new list that will be half the length of the original list.
  • We will repeat steps 1, 2, 3, and 4 on this new list.
  • We will continue this until we find the element, or else there is no more element left in the list to check for.
  • This algorithm will return the index of the target number in the list. If the element is not found in the list, i.e., the loop ends without breaking in between, it will return -1.

Binary search algorithms have a time complexity of O(log(n)).

Binary search First Occurrence

Now let's suppose the target number we want to find in the list is present more than once. So, in this case, we want to find the first occurrence of the target number in the list.

The algorithm to solve this problem is as follows:-

  • Let's suppose we have a sorted list of integers, and we know that in the list, there are numbers present more than once in the list. Let the list be ints.
  • Now we must find the first occurrence of the number x in the list.
  • We will use two pointers which will point to the starting and the ending indices of the list. We need to update these pointers every time we discard a certain half of the list. The new list will be between the indices of these two pointers. We will initialize these pointers as s for starting, equal to 0, and e for the end, equal to the last index, i.e., length of the list -1 in Python.
  • We have to search the number x in the given array of length 'length'. We will define a function that will search the first occurrence of x from the given array. The function will take the array, the length of the array, and the number that needs to be searched as its parameters.
  • Inside the function, we have initialized a variable result. This will store the index whenever the pointer finds x in the array. The default value given to the variable is -1 because if the pointer cannot find any x in the array, it will return -1. This was the rule which we needed to be followed.
  • Now we will start a while loop. The while loop will run until the starting index s is less than or equal to e. The loop will stop when the lower index exceeds the higher index.
  • Inside the while loop, we have initialized a variable mid equal to (s + e) // 2. This variable will find the middle index of the list bounded by the index s and e.
  • Now we will check some conditions to help us decide which half of the array we must discard. If the value present at the middle index' mid' of the array is equal to the number, we need to search then we will store the index in the result. Now if the element is present at the middle index of the array and we have to find the first occurrence of the given number, we have to search in the left half of the array. Next, if the middle value is greater than the number we need to search, then we have to search in the left half of the array. If the middle value is less than the number, we need to search, and then we have to search in the right half of the array. In this way, the loop will continue until both s and e point to a single value. When the loop ends, the variable result will have the index of the first occurrence of the variable.
  • If there is no x in the array, the value of the result variable will remain unchanged, and hence it will return -1.

Now we will see how to implement this algorithm in Python.

Code

Output:

The first occurrence of 3 is at: 3

Now we will try to search for a number that is not present in the list.

Code

Output:

The first occurrence of 3 is at: -1

This algorithm has a time complexity of O(log(n)). Hence, we have optimized the solution. We have seen a total of three ways to find the index of the first occurrence of an element.






Latest Courses