C PGM - Unit - 3
C PGM - Unit - 3
The algorithm for linear search is relatively simple. The procedure starts at the
very first index of the input array to be searched.
Step 1 − Start from the 0th index of the input array, compare the key value with
the value present in the 0th index.
Step 2 − If the value matches with the key, return the position at which the
value was found.
Step 3 − If the value does not match with the key, compare the next element in
the array.
Step 4 − Repeat Step 3 until there is a match 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.
#include <stdio.h> • Linear search can be used irrespective
void linear_search(int a[], int n, int key){
int i, count = 0; of whether the array is sorted or not.
for(i = 0; i < n; i++) { • It can be used on arrays of any data
if(a[i] == key) { // compares each element of the array
printf("The element is found at %d position\n", type.
i+1);
count = count + 1;
• Does not require any additional
} memory.
}
if(count == 0) // for unsuccessful search
• It is a well-suited algorithm for small
printf("The element is not present in the array\n"); datasets.
}
int main()
{
int i, n, key;
n = 6;
int a[10] = {12, 44, 32, 18, 4, 10};
key = 18;
linear_search(a, n, key);
key = 23;
linear_search(a, n, key);
return 0;
}
BINARY SEARCH
This search algorithm works on the principle of divide and conquer.
This algorithm to work properly, the data collection should be in the sorted
form.
Search a sorted array by repeatedly dividing the search interval in half.
Binary search looks for a particular item by comparing the middle most item
of the collection. If a match occurs, then the index of item is returned.
If the middle item is greater than the item, then the item is searched in the
sub-array to the left of the middle item.
Otherwise, the item is searched for in the sub-array to the right of the middle
item.
This process continues on the sub-array as well until the size of the subarray
reduces to zero.
#include<stdio.h> { pos = mid;
main() c = 1;
{
break;
int a[10], n, i, key, low = 0, high, mid, pos=0, c=0;
}
printf("Enter the number of elements \n");
scanf("%d", &n); else if(a[mid] > key)
printf("Enter the array elements \n"); high = mid - 1;
for(i=0 ; i<n ; i++) else
scanf("%d", &a[i]); low = mid + 1;
printf("Enter the element to be searched \n"); }
scanf("%d", &key);
if(c == 0)
high = n-1;
printf("Element not found \n");
while(low <= high)
{
else
mid = (low + high) / 2; printf("Element found at
if(a[mid] == key)
position %d \n", pos);
return 0;
}
• A function is a self-contained block of code that
performs a specific task or set of tasks.
• Functions are used to break down a program into
smaller, manageable pieces, making the code more
modular and easier to understand.
Defining a function:
• To define a function in C, you use the following syntax:
return_type function_name(parameter1_type
parameter1_name, parameter2_type parameter2_name, ...)
{
// Function body