Binary Search, Bubble Sort, Insertion Sort
Binary Search, Bubble Sort, Insertion Sort
#include <stdio.h>
int main(void) {
int mid,first,last,n,a[100],k,i;
printf("\n enter the no of elements") ;
scanf("%d",&n);
printf("\n enter the values");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the element to be searched");
scanf("%d",&k);
first=0;
last=n-1;
mid=(first+last)/2;
while(first<=last)
{
if(a[mid]<k)
first=mid+1;
else if(a[mid]==k)
{
printf("%d element is in middle",k);
break;
}
else
last =mid-1;
mid=(first+last)/2;
}
return 0;
}
Bubble sort
#include<stdio.h>
int main()
{
int i,j,temp;
int a[10]={10,9,7,101,23,44,12,78,34,25};
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n sorted elements are:");
for(i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
return 0;
}