- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Python Program to Search an element in an Array

In Python, there are mainly two searching algorithms that are majorly used. Out of those, the first one is Linear Search and the second one is Binary Search.

These two techniques are majorly used in order to search an element from the given array or from the given list also. While searching an element, there are two methodologies that can be followed in any kind of algorithm. One of those is recursive approach and the other is iterative approach. Let us discuss both algorithms in both approaches and solve similar problems.

## Linear Search

The Linear Search technique is also known as Sequential search. The meaning of the name “ Sequential search ” is definitely justified by the process followed by this search algorithm. It is a method or technique which is used in order to find the elements within an array or a list in Python.

It is known to be the most simplest and easiest of all the other searching algorithms. But, the only drawback of this algorithm is that it is not so efficient. That is the main reason for not using Linear search very frequently.

### Algorithm

**Step 1**− It searches for an element in a sequential order just by comparing the desired element with each element present in the given array.**Step 2**− If the desired element is found, then the index or position of the element will be displayed to the user.**Step 3**− If the element is not present within the array, then the user will be informed that the element is not found. In this way, the algorithm is processed.

In general, Linear search algorithm is comparatively suitable and efficient for small arrays or small lists which has a size less than or equal to 100 as it checks and compares with each element.

More time will be consumed if the desired element is present in the last position of the array.

The Time complexity of Linear Search algorithm in best case is “ O( 1 ) ”. In this case, the element will be present in the first position of the array, i.e., with the index “ 0 ”.

The Time complexity of Linear Search algorithm in average case is “ O( n ) ”. In this case, the element will be present in the middle position of the array, i.e., with the index “ ( n – 1 ) / 2 ” or “ (( n – 1 ) / 2 )+ 1 ”.

The Time complexity of Linear Search algorithm in worst case is “ O( n ) ”. In this case, the element will be present in the last position of the array, i.e., with the index “ n-1 ”.

### Example

In the following example, we are going to learn about the process of searching an element in an array using Linear search.

def iterative_linear( arr, n, key_element): for x in range(n): if(arr[x] == key_element): return x return -1 arr = [2, 3, 5, 7, 9, 1, 4, 6, 8, 10] max_size = len(arr) key = 8 result = iterative_linear(arr, max_size - 1, key) if result != -1: print ("The element", key," is found at the index " ,(result), "and in the ", (result+1), "position") else: print ("The element %d is not present in the given array" %(key))

### Output

The output for the above program is as follows −

The element 8 is found at the index 8 and in the 9 position

### Example (Recursive)

In the following example, we are going to learn about the process of searching an element in an array using Linear search in recursive approach.

def recursive_linear( arr, first_index, last_index, key_element): if last_index < first_index: return -1 if arr[first_index] == key_element: return first_index if arr[last_index] == key_element: return last_index return recursive_linear(arr, first_index + 1, last_index - 1, key_element) arr = [2, 3, 5, 7, 9, 1, 4, 6, 8, 10] max_size = len(arr) key = 8 result = recursive_linear(arr, 0, max_size - 1, key) if result != -1: print ("The element", key," is found at the index " ,(result), "and in the ", (result+1), "position") else: print ("The element %d is not present in the given array" %(key))

### Output

The output for the above program is as follows −

The element 8 is found at the index 8 and in the 9 position

## Binary Search

The Binary search algorithm is quite different from the Linear search algorithm. It follows completely different procedure in order to search an element from the array. It only considers sorted arrays generally.

If the array is not sorted in some cases, the array is sorted and then the procedure of the Binary search algorithm starts. As soon as the array is considered by the Binary search algorithm, it is sorted first and then the algorithm is applied on the array.

### Algorithm

**Step 1**− The process of sorting the array is the first step followed.**Step 2**− After the array is sorted, the array is considered as two halves. One half is starting from the first element to the middle element of the sorted array and the second half is starting from the element after the middle element to the last element of the sorted array.**Step 3**− The key element (the element that is supposed to be searched is known as key element) is compared with the middle element of the sorted array.**Step 4**− If the key element is less than or equal to the middle element of the sorted array, the second half elements are ignored further as the key element is smaller than the middle element. So, definitely, the element must be present in between the first element and the middle element.**Step 6**− If the key element is greater than the middle element, then the first half of the sorted array is ignored and the elements from the middle element to the last element are considered.**Step 7**− Out of those elements, the key element is again compared with the middle element of the halved array and repeats the same procedure. If the key element is greater than the middle element of the halved array, then the first half is neglected.**Step 8**− If the key element is less than or equal to the middle element of the halved array, the second half of the halved array will be neglected. In this way, the elements are searched in any half of the array accordingly.

So, when compared to the Linear search, the complexity is reduced by half or more than half as half of the elements will be removed or not considered in the first step itself. The best case time complexity of Binary search is “ O(1) ”. The worst case time complexity of Binary search is “ O(logn) ”. This is how the algorithm of binary search works out. Let us consider an example and apply the Binary search algorithm to find out the key element out of the elements present in the array.

### Example

In this example, we are going to learn about the process of searching an element in an array using Binary search in recursive approach.

def recursive_binary(arr, first, last, key_element): if first <= last: mid = (first + last) // 2 if arr[mid] == key_element: return mid elif arr[mid] > key_element: return recursive_binary(arr, first, mid - 1, key_element) elif arr[mid] < key_element: return recursive_binary(arr, mid + 1, last, key_element) else: return -1 arr = [20, 40, 60, 80, 100] key = 80 max_size = len(arr) result = recursive_binary(arr, 0, max_size - 1, key) if result != -1: print("The element", key, "is present at index", (result), "in the position", (result + 1)) else: print("The element is not present in the array")

### Output

The output for the above program is as follows −

The element 80 is found at the index 3 and in the position 4

### Example

In this example, we are going to learn about the process of searching an element in an array using Binary search in iterative approach.

def iterative_binary(arr, last, key_element): first = 0 mid = 0 while first <= last: mid = (first + last) // 2 if arr[mid] < key_element: first = mid + 1 elif arr[mid] > key_element: last = mid - 1 else: return mid return -1 arr = [20, 40, 60, 80, 100] key = 80 max_size = len(arr) result = iterative_binary(arr, max_size - 1, key) if result != -1: print("The element", key, "is present at index", (result), "in the position", (result + 1)) else: print("The element is not present in the array")

### Output

The output for the above program is as follows −

The element 80 is found at the index 3 and in the position 4

This is how Binary Search algorithm works. We can definitely say that Binary Search algorithm is more efficient than the Linear Search algorithm based on the concept of Time complexity which plays a major role. In this way, an element of an array can be searched by using such type of algorithms. Though the procedure used to solve the problem varies, the result will not fluctuate. This is one advantage of using several algorithms to check the consistency of the output.