8 Searching
8 Searching
Sequential searching is nothing but searching an element in linear way. This can be in
array or linked list. Its start the search from beginning and scan the elements one by one
until the end of array or linked list. If search is successful then it will return the location
of element, otherwise it will return the failure notification.
Algorithm
Step 1- INDEX=0
Step 2- Scan each element of array one by one.
Step 3- a) If match occurs then return the index value,
b) Otherwise INDEX=INDEX+1
Step 4- Repeat the same process until unique value comes in scanning.
Step 5- Return the failure notification.
//Linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int data,i,flag=0;
int array[100];
clrscr();
printf("\nHow many elements you want to be entered :");
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf("\nenter the number :");
scanf(“%d”,&array[i]);
}
printf("\nenter the number to be searched :");
scanf(“%d”,&data);
for(i=0;i<n;i++)
{
if(array[i]==data)
flag=1;
}
if(flag==1)
printf("\n Number is found");
else
printf("\n Number is not found");
getch();
}
Binary Search
The condition of binary search is that all the data should be in sorted array. We compare
the element with middle element of the array. If it is less than the middle element then we
search it in the left portion of the array and if it is greater than the middle element then
we search it in the left portion of the array and if it is greater than the middle element
then search will be in the right portion of the array. Now we will take that portion only
for search and compare with middle element of that portion. This process will be in
iteration until we find the element or middle element has no left or right portion to search.
Ex- arr [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
10 18 19 20 25 30 49 57 64 72
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],start,end,mid,n,i,item;
clrscr();
printf("\nHow many elements u want to enter in the array :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element %d :",i+1);
scanf("%d",&arr[i]);
}
printf("\nEnter the element to be searched :");
scanf("%d",&item);
start=0;
end=n-1;
mid=(start+end)/2;
while(item!=arr[mid] && start<=end)
{
if(item>arr[mid])
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
}
if(item==arr[mid])
{
printf("%d found at position %d\n",item,mid+1);
}
else
{
printf("%d not found in array\n",item);
}
getch();
}
Sorting
Ex- 13 32 20 62 68 52 38 46
#include<stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int arr[MAX],i,j,temp,n;
clrscr();
printf("\nEnter the number of elements :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\nUnsorted list is :\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
/*Linear sort */
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nSorted list is :\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
getch();
}
Bubble Sort
// Bubble sort
#include<stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int arr[MAX],i,j,k,temp,n;
clrscr();
printf("\nEnter the number of elements :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\nUnsorted list is :\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
/*Bubble sort */
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nSorted list is :\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
getch();
}
// Insertion sort
#include<stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int arr[MAX],i,j,k,n;
clrscr();
printf("\nEnter the number of elements :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\nUnsorted list is :\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
/*Insertion sort */
for(j=1;j<n;j++)
{
k=arr[j];
for(i=j-1;i>=0 && k<arr[i];i--)
{
arr[i+1]=arr[i];
}
arr[i+1]=k;
}
printf("\nSorted list is :\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
getch();
}