SEARCHING IN AN ARRAY
• Searching is the process of
finding a given value in a list of
values.
• It decides whether a search key is
present in the data or not. TWO TYPES OF SEARCHING
1. Linear search
• It is the algorithmic process of 2. Binary search
finding a particular item in a
collection of items.
Slides credit: GitHub - nkr1234/C-Programming-ppt
SEARCHING IN AN ARRAY
Linear Search
• Linear search is also called as sequential search algorithm
• Searching for the key element is done in a linear fashion.
• It is the simplest searching technique.
• 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.
• It is widely used to search an element from the unordered list, i.e., the list in which
items are not sorted
SEARCHING IN AN ARRAY – LINEAR SEARCH
Step 1: Start
Step 2: [Initialize counter variable. ] Set i = 0
Step 3: Repeat Step 4 and 5 for i = 0 to i < n
Step 4: if a[i] = x, then jump to step 7
Step 5: [Increase counter. ] Set i = i + 1
Step 6: [End of step 3 loop. ]
Step 7: Print x found at i + 1 position and go to step 09
Step 8: Print x not found (if a[i] != x, after all the iteration of the above for loop. )
Step 9: Stop
//PROGRAM TO SEARCH AN ELEMENT IN AN ARRAY
// program to search an element in an array printf("\nEnter the element to search: ");
#include<stdio.h> scanf("%d", &x);
#define MAX 50
int main() for(i = 0; i < n; i++)
{ {
int i, n, x; if(a[i] == x)
{
int a[MAX]; printf("%d found at %d position.", x, i+1);
printf("Enter the size of the array: "); return 0;
scanf("%d", &n); }
}
printf("\nEnter %d elements:\n", n);
printf("%d not found", x);
for(i = 0; i < n; i++)
scanf("%d\n", &a[i]); return 0;
}
SEARCHING IN AN ARRAY
Binary Search is a search algorithm that is used to find the position of an element (target
value ) in a sorted array. The array should be sorted prior to applying a binary search.
Working
The binary search algorithm works by comparing the element to be searched by the middle
element of the array and based on this comparison follows the required procedure.
Case 1 − element = middle, the element is found return the index.
Case 2 − element > middle, search for the element in the sub-array starting from middle+1
index to n.
Case 3 −element < middle, search for element in the sub-array starting from 0 index to
middle -1.
BINARY SEARCH IN AN ARRAY
Step 1 : Find the middle element of array. using ,
middle = start + end / 2 ;
Step 2 : If middle = element, return ‘element found’ and index.
Step 3 : if middle > element, call the function with end = middle - 1 .
Step 4 : if middle < element, call the function with start = middle + 1 .
Step 5 : exit.
//PROGRAM FOR BINARY SEARCH IN AN ARRAY
#include <stdio.h> int main( )
int binarySearch(int array[], int start, int end, int {
element) int array[] = {1, 4, 7, 9, 16, 56, 70};
{ int n = 7;
while (start <= end){ int element = 16;
int middle = start + (end- start)/2; int found_index = binarySearch(array, 0, n-1,
if (array[middle] == element) element);
return middle; if(found_index == -1 )
if (array[middle] < element) {
start = middle + 1; printf("Element not found in the array ");
else }
end= middle - 1; else
} {
return -1; printf("Element found at : %d",found_index);
} }
return 0;
}