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

Linear and Binary Search

The document discusses searching algorithms, specifically Linear (Sequential) Search and Binary Search. Linear Search checks each element in a list sequentially, while Binary Search requires a sorted list and divides the search space in half with each step. The time complexities for these searches are O(n) for the worst case in Linear Search and O(log2n) for the worst case in Binary Search.

Uploaded by

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

Linear and Binary Search

The document discusses searching algorithms, specifically Linear (Sequential) Search and Binary Search. Linear Search checks each element in a list sequentially, while Binary Search requires a sorted list and divides the search space in half with each step. The time complexities for these searches are O(n) for the worst case in Linear Search and O(log2n) for the worst case in Binary Search.

Uploaded by

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

Searching & Sorting

Data Structure
Searching
• Searching is an operation which finds the
position of a given element in the list.
• The search process is set to be successful or
unsuccessful depending upon the searched
element is found or not.
• Classified into two types:
Linear (sequential) Search
Binary Search
Linear (Sequential) Search

key=8

[0] [1] [2] [3] [4] [5] [6]

3 1 7 45 32 8 53
a
Linear (Sequential) Search

key=8

[0] [1] [2] [3] [4] [5] [6]

3 1 7 45 32 8 53

Is key==a[0] ? NO
Linear (Sequential) Search

key=8

[0] [1] [2] [3] [4] [5] [6]

3 1 7 45 32 8 53

Is key==a[1] ? NO
Linear (Sequential) Search

key=8

[0] [1] [2] [3] [4] [5] [6]

3 1 7 45 32 8 53

Is key==a[2] ? NO
Linear (Sequential) Search

key=8

[0] [1] [2] [3] [4] [5] [6]

3 1 7 45 32 8 53

Is key==a[3] ? NO
Linear (Sequential) Search

key=8

[0] [1] [2] [3] [4] [5] [6]

3 1 7 45 32 8 53

Is key==a[4] ? NO
Linear (Sequential) Search

key=8

[0] [1] [2] [3] [4] [5] [6]

3 1 7 45 32 8 53

Is key==a[5] ? YES Found


Linear (Sequential) Search
• ALGORITHM
Linear_search(a[],size,value)
{
Start with the first array element (index 0)
while(more elements in array){
if value found at current index, return
index;
Try next element (increment index);
}
Value not found, return -1;
}
Linear (Sequential) Search
• ALGORITHM (or)

Linear_search(a[],size,value)
1. for(i=0;i<size;i++)
2. if(value==a[i])
3. return index;
4. return (-1); (Value not found)
end.
Linear (Sequential) Search
• Time Complexity
Best Case
If the searched element is present at the first
place i.e. at a[0], then
T(n)=O(1)

Worst Case
If the searched element is present at the end of
the list i.e. at a[n-1] or it is not present in the list
T(n)=O(n)
Binary Search

• The only prerequisite for binary search is, the


list must be in sorted order.
In the Increasing order .
or
In the Decreasing order.
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.
Binary Search
• Algorithm:
binary_search(a[], size, value)
1. low=0,high=size-1;
2. while(low<=high)
3. mid=(low+high)/2;
4. if(value==a[mid])
5. return(mid);
6. if(value<a[mid])
7. high=mid-1;
8. if(value>a[mid])
9. low=mid+1;
10. return(-1); (Value not found)
end
Binary Search
• Time Complexity
Best Case
If the searched element is present at the middle
place i.e. at a[mid], then
T(n)=O(1)

Worst Case
T(n)=O(log2n)
Binary Search
• How T(n)=O(log2n) ?
T(n) = T(n/2) + 1 [1 to find the mid, n/2 for compare with n/2 ele]

-----------(1)
From (1)
T(n/2) = T(n/4) + 1 -----(2)
From (1) and (2)
T(n) = T(n/4) + 2
Again from (1)
T(n/4) = T(n/8) + 1
From the previous equations
T(n) = T(n/8)+3 = T(n/23 ) + 3
----------------------------------------
= T(n/2k) + k -------(3)
After k division only one element left so
n/2k =1 => n = 2k => k=log2n
Now put the value of k into (3)
T(n) = T(1) + log2n = log2n so,
T(n)= O(log2n)
Program-C
• Global Declaration

• main()

• linear_search()

• binary_search()
Thank You

You might also like