0% found this document useful (0 votes)
182 views

Linear Search

This document from the Department of Computer Science and Engineering discusses linear and binary search algorithms. It provides an overview of linear search, describing how it searches unsorted lists sequentially until the target element is found. The document also includes pseudocode for linear search and analyzes its O(n) complexity. Binary search is then introduced as a way to search sorted lists by repeatedly dividing the search space in half. Pseudocode and analysis of binary search's O(log n) complexity are also provided.

Uploaded by

akash chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Linear Search

This document from the Department of Computer Science and Engineering discusses linear and binary search algorithms. It provides an overview of linear search, describing how it searches unsorted lists sequentially until the target element is found. The document also includes pseudocode for linear search and analyzes its O(n) complexity. Binary search is then introduced as a way to search sorted lists by repeatedly dividing the search space in half. Pseudocode and analysis of binary search's O(log n) complexity are also provided.

Uploaded by

akash chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Computer Science and Engineering (CSE)

Linear Search and Binary Search

PREPARED BY: TARU BHATNAGAR

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

OUTCOMES
Linear search Algorithm
Complexity analysis
Binary search normal description

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Linear search
A linear search is the basic and simple search algorithm.
A linear search searches an element or value from an array till
the desired element or value is not found and it searches in a
sequence order.
It compares the element with all the other elements given in the
list and if the element is matched it returns the value index else
it return -1.
Linear Search is applied on the unsorted or unordered list when
there are fewer elements in a list.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

LINEAR SEARCH ALGORITHM


There is an array A of size N and we have to find an element x in the array
1.Start
2. Set J = 0 and f=0
3. Repeat steps 4 and 5 while J <N
4. Check if A[j] equals x then goto step 7
5. set j=j+1
6. If j equals N go to step 8.
7. set f=1 and print the location j
8. check if (f equals 0) then print element not found
9. Stop

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Complexity
O(n)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Binary search
Binary Search: Search a sorted array by repeatedly dividing the search
interval in half. Begin with an interval covering the whole array.
If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half.
Otherwise narrow it to the upper half. Repeatedly check until the value
is found or the interval is empty.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Binary search algorithm

1 def binarySearch(A, x):


2 n = len(A)
3 beg = 0
4 end = n 1
5 while (beg <= end) repeat steps 6,7,8
6 mid = (beg + end) / 2
7 if (A[mid]==x go to step 9
8 else if (A[mid] <x) do beg = mid + 1 otherwise end = mid - 1
9 return mid.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

complexity

O(log n)
For an array of size N, it eliminates until 1 element
remains. N, N/2, N/4, N/8, ..., 4, 2, 1 How many divisions
does it take?
0->N
1->N/2

Divide and conquer approach.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Queries??

University Institute of Engineering (UIE)

You might also like