Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Algorithms  >  Linear Search & Binary Search- 1

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE) 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 | Algorithms - Computer Science Engineering (CSE)
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 | Algorithms - Computer Science Engineering (CSE)


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). 

Question for Linear Search & Binary Search- 1
Try yourself:
What is another name for the Linear Search algorithm?
View Solution

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)However, 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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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

Code Implementation:


C

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)
C++
Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)Python

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)
Java
Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE) 

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 | Algorithms - Computer Science Engineering (CSE)

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.

Question for Linear Search & Binary Search- 1
Try yourself:
Which algorithm is based on the principle of divide and conquer and requires the data collection to be in sorted form?
View Solution

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

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

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

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

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

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 | Algorithms - Computer Science Engineering (CSE)

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

C++
Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

Python
Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

Java
Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)


Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

The document Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Linear Search & Binary Search- 1 - Algorithms - Computer Science Engineering (CSE)

1. What is the difference between Linear Search and Binary Search?
Ans. Linear search involves sequentially checking each element of a list until a match is found, while binary search requires the list to be sorted and repeatedly dividing the search interval in half until the value is found.
2. When should I use Linear Search over Binary Search?
Ans. Linear search is preferred when the list is unsorted or when the list size is small. Binary search is more efficient for searching in sorted lists.
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.
4. What is the time complexity of Binary Search?
Ans. The time complexity of Binary Search is O(log n), where n is the number of elements in the list.
5. Can Binary Search be performed on an unsorted list?
Ans. No, Binary Search requires the list to be sorted in order to work correctly.
81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

Exam

,

practice quizzes

,

Objective type Questions

,

video lectures

,

Important questions

,

Extra Questions

,

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

,

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

,

shortcuts and tricks

,

mock tests for examination

,

Free

,

Summary

,

Linear Search & Binary Search- 1 | Algorithms - Computer Science Engineering (CSE)

,

pdf

,

ppt

,

Sample Paper

,

Previous Year Questions with Solutions

,

MCQs

,

study material

,

past year papers

,

Semester Notes

;