GATE Exam  >  GATE Notes  >  Linear Search & Binary Search- 1

Linear Search & Binary Search- 1 - GATE PDF Download

Linear search is also called as sequential search algorithm. It is the simplest searching algorithm. In Linear search, we simply traverse the list completely and match each element of the list with the item whose location is to be found. If the match is found, then the location of the item is returned; otherwise, the algorithm returns NULL.

Linear Search & Binary Search- 1 - GATE
For example in above diagram, we are searching for an element 33. Therefore, the linear search method searches for it sequentially from the very first element until it finds a match. This returns a successful search. 

Linear Search Algorithm

Step 1: Start from the 0th index of the input array, comparing the key value with the value at the 0th index.

Step 2: If the value matches the key, return the position at which the value was found.

Step 3: If the value does not match the key, compare the next element in the array.

Step 4: Repeat Step 3 until a match is found. Return the position at which the match was found.

Step 5: If it is an unsuccessful search, print that the element is not present in the array and exit the program.

Pseudocode

Linear Search & Binary Search- 1 - GATE


Analysis

  • Best Case: Linear search sequentially traverses through every element, making the best-case time complexity O(1) when the element is found in the first iteration.
  • Worst Case: In the case of an unsuccessful search, where the key value is not found, linear search performs n iterations, resulting in a worst-case time complexity of O(n).
  • Average Case: The average case time complexity of linear search is O(n). 

Example: Let us look at the step-by-step searching of the key element (say 47) in an array using the linear search method.

Linear Search & Binary Search- 1 - GATE

Step 1: The linear search starts from the 0th index. Compare the key element with the value in the 0th index, 34.

Linear Search & Binary Search- 1 - GATEHowever, 47 ≠ 34. So it moves to the next element.

Step 2: Now, the key is compared with value in the 1st index of the array.

Linear Search & Binary Search- 1 - GATE

Still, 47 ≠ 10, making the algorithm move for another iteration.

Step 3: The next element 66 is compared with 47. They are both not a match so the algorithm compares the further elements.

Linear Search & Binary Search- 1 - GATE

Step 4: Now the element in 3rd index, 27, is compared with the key value, 47. They are not equal so the algorithm is pushed forward to check the next element.

Linear Search & Binary Search- 1 - GATE

Step 5: Comparing the element in the 4th index of the array, 47, to the key 47. It is figured that both the elements match. Now, the position in which 47 is present, i.e., 4 is returned.

Linear Search & Binary Search- 1 - GATE

The output achieved is “Element found at 4th index”.

Code Implementation:


C

Linear Search & Binary Search- 1 - GATELinear Search & Binary Search- 1 - GATE
C++
Linear Search & Binary Search- 1 - GATELinear Search & Binary Search- 1 - GATEPython

Linear Search & Binary Search- 1 - GATE

Linear Search & Binary Search- 1 - GATE
Java
Linear Search & Binary Search- 1 - GATE 

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer, since it divides the array into half before searching. For this algorithm to work properly, the data collection should be in the sorted form. 

Linear Search & Binary Search- 1 - GATE

Binary search aims to find a specific key value by examining the middle item of a collection. If a match is found, it returns the index of that item. However, if the middle item's value is higher than the key value, it looks in the right sub-array. Otherwise, it checks the left sub-array. This process continues recursively until the size of the subarray becomes zero.

  • Binary search locates a target value by checking the middle item in the collection.
  • If there's a match, it gives back the index of the item.
  • If the middle item's value is greater than the target value, it searches the right sub-array.
  • If the middle item's value is less than the target value, it searches the left sub-array.
  • This procedure repeats recursively until the subarray size becomes zero.

Binary Search Algorithm

Step 1: Select the middle item in the array and compare it with the target value. If they match, return the position of the middle item.

Step 2: If there's no match, determine whether the target value is greater than or less than the middle item's value.

Step 3: If the target value is greater, search in the right sub-array. If it's lower, search in the left sub-array.

Step 4: Repeat Steps 1, 2, and 3 iteratively until the size of the sub-array becomes 1.

Step 5: If the target value is not found in the array, the search is considered unsuccessful.

Pseudocode

Linear Search & Binary Search- 1 - GATE

Analysis


The time complexity of the binary search algorithm is O(log n).

For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the process of binary search with a pictorial example. The following is our sorted array and let us assume that we need to search the location of value 31 using binary search.

Linear Search & Binary Search- 1 - GATE

First, we shall determine half of the array by using this formula −

mid = low + (high - low) / 2

Here it is, 0 + (9 - 0) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

Linear Search & Binary Search- 1 - GATE

Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array, so we also know that the target value must be in the upper portion of the array.

Linear Search & Binary Search- 1 - GATE

We change our low to mid + 1 and find the new mid value again.

low = mid + 1 mid = low + (high - low) / 2

Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

Linear Search & Binary Search- 1 - GATE

The value stored at location 7 is not a match, rather it is less than what we are looking for. So, the value must be in the lower part from this location.

Linear Search & Binary Search- 1 - GATE

Hence, we calculate the mid again. This time it is 5.

Linear Search & Binary Search- 1 - GATE

We compare the value stored at location 5 with our target value. We find that it is a match.

Linear Search & Binary Search- 1 - GATE

We conclude that the target value 31 is stored at location 5.

Binary search halves the searchable items and thus reduces the count of comparisons to be made to very less numbers.

Code Implementation

C
Linear Search & Binary Search- 1 - GATE

Linear Search & Binary Search- 1 - GATE

C++
Linear Search & Binary Search- 1 - GATE

Linear Search & Binary Search- 1 - GATE

Python
Linear Search & Binary Search- 1 - GATE

Linear Search & Binary Search- 1 - GATE

Java
Linear Search & Binary Search- 1 - GATE


Linear Search & Binary Search- 1 - GATE

The document Linear Search & Binary Search- 1 - GATE is a part of GATE category.
All you need of GATE at this link: GATE

FAQs on Linear Search & Binary Search- 1 - GATE

1. What is linear search?
Ans. Linear search is a simple searching algorithm that sequentially checks each element in a list to see if it matches the target value. It starts at the beginning and moves through the entire list until it finds a match or reaches the end.
2. How does linear search work?
Ans. Linear search works by comparing each element in a list with the target value. It starts at the first element and checks if it is a match. If not, it moves to the next element and repeats the process until a match is found or the end of the list is reached.
3. What is the time complexity of linear search?
Ans. The time complexity of linear search is O(n), where n is the number of elements in the list. This means that the time taken to perform a linear search increases linearly with the size of the list.
4. When is linear search most suitable?
Ans. Linear search is most suitable when the list is small or unsorted. It is also useful when the position of the target element is not important, and only its presence or absence needs to be determined.
5. What are the advantages and disadvantages of linear search?
Ans. The advantages of linear search are its simplicity and ability to work on unsorted lists. However, it is not efficient for large lists and may require checking every element in the worst case scenario.
Download as PDF

Top Courses for GATE

Related Searches

Free

,

Viva Questions

,

past year papers

,

mock tests for examination

,

Exam

,

Linear Search & Binary Search- 1 - GATE

,

study material

,

pdf

,

Extra Questions

,

shortcuts and tricks

,

practice quizzes

,

MCQs

,

Previous Year Questions with Solutions

,

video lectures

,

Objective type Questions

,

Important questions

,

Sample Paper

,

Semester Notes

,

ppt

,

Linear Search & Binary Search- 1 - GATE

,

Summary

,

Linear Search & Binary Search- 1 - GATE

;