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

Pps Source 3

Uploaded by

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

Pps Source 3

Uploaded by

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

Searching in C

Digember Kumar
Assistant Professor
Department of Computer Science and Engineering

Edited on: October-16, 2024


Searching Algorithms:
• Searching Algorithms are designed to check for an element or retrieve an element from
any data structure where it is stored.
• Searching is the fundamental process of locating a specific element or item within a
collection of data.
• Two fundamental types of searching algorithms are: Linear search and binary search.
Linear Search Algorithm:
Linear Search:
A linear search, also known as a sequential search, is a method of finding
an element within a list. It checks each element of the list sequentially until
a match is found or the whole list has been searched.

Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170}
x = 110;
Output : 6
Element x is present at index 6

Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170}
x = 175;
Output : -1
Element x is not present in arr[].
Algorithm:
Start from the leftmost element of arr[] and one by one compare x with each element of
arr[].
If x matches with an element, return the index.
If x doesn’t match with any of elements, return -1.
Pseudocode for Linear Search:
a[]: Array of elements
n: no. of elements
x: key
int linearSearchAlgo(a[], x, n){
for(i=0; i<n; i++){
if(a[i] == x)
break;
}
//Element found at index i
if(i < n){
return i;
}
//Element not found
return -1;
}
Advantages of Linear Search Algorithm:
• Linear search can be used irrespective of whether the array is sorted or not.
• It can be used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.

Disadvantages of Linear Search Algorithm:


• Linear search has a time complexity of O(N), which in turn makes it slow for large
datasets.
• Not suitable for large arrays.
Binary Search In C:
• Binary Search Algorithm is a searching algorithm used in a sorted array
by repeatedly dividing the search interval in half.
• The idea of binary search is to use the information that the array is sorted and
reduce the time complexity to O(log2N).
Binary Search Algorithm in C:
• Divide the search space into two halves by finding the middle index “mid”.
• Compare the middle element of the search space with the key.
• If the key is found at middle element, the process is terminated.
• If the key is not found at middle element, choose which half will be used as the
next search space.
• If the key is smaller than the middle element, then the left side is used for next
search.
• If the key is larger than the middle element, then the right side is used for next
search.
• This process is continued until the key is found or the total search space is
exhausted.
Algorithm:
1. The highest and the lowest value are added and divided by 2.
2. The mid value is then compared with the key.
3. If mid is equal to the key, then we get the output directly.
4. Else if the key is greater then mid then the mid+1 becomes the lowest value and the
process is repeated on the shortened array.
5. Else if the key value is less then mid, mid-1 becomes the highest value and the process
is repeated on the shortened array.
6. If it is not found anywhere, an error message is displayed.
Binary Search pseudocode:
list[]: sorted array, key: element to be searched, len: number of elements in the array
int binSearchAlgo(list[], len){
idxL = 0, idxH = len;
while(idxL<idxH){
idxM = (idxL+idxH)/2;
if(key == list[idxM]){
return idxM;
}
else if(key>list[idxM]){
idxL= idxM+1;
}
else{
idxH= idxM;
}
} //End of while
return -1; } //End of binSearchAlgo
Advantages of Binary Search
• Binary search is faster than linear search, especially for large arrays.
• Binary search is well-suited for searching large datasets.

Disadvantages of Binary Search


• Binary search requires that the data structure being searched be stored.
• Binary search requires that the elements of the array be comparable, meaning that they
must be able to be ordered.
Comparison:
Linear Search Binary Search

In linear search input data need not to be in sorted. In binary search input data need to be in sorted order.

It is also called sequential search. It is also called half-interval search.

The time complexity of linear search O(n). The time complexity of binary search O(log n).

It is less complex. It is more complex.

It is very slow process. It is very fast process.

You might also like