Saturday, 4 November 2017

C Program to sort given set of numbers in ascending/descending order using Bubble sort and also search a number using binary search.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void bubble_sort(int a[],int);
void binary_search(int a[],int, int);
void main()
{
int data[100],i,n,element,ch;
while(1)
{
clrscr();
printf("****MENU****\nEnter 1 to apply bubble sort\nEnter 2 to search an element using Binary Search\nEnter 3 for exit\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
break;
case 2:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
printf("Enter the element you want to search for\n");
scanf("%d",&element);
binary_search(data,n,element);
break;
case 3:
exit(0);
default:
printf("You have entered wrong choice\n");
}
getch();
}
}
void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting array elements are:- \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
                              }
void binary_search(int a[],int n,int element)
{
int beg,end,mid;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(element==a[mid])
{
printf("Element found at location %d\n",mid+1);
return;
}
else if(element>a[mid])
beg=mid+1;
else
end=mid-1;
}
printf("Element not found\n");
return;
                              }#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void bubble_sort(int a[],int);
void binary_search(int a[],int, int);
void main()
{
int data[100],i,n,element,ch;
while(1)
{
clrscr();
printf("****MENU****\nEnter 1 to apply bubble sort\nEnter 2 to search an element using Binary Search\nEnter 3 for exit\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
break;
case 2:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
printf("Enter the element you want to search for\n");
scanf("%d",&element);
binary_search(data,n,element);
break;
case 3:
exit(0);
default:
printf("You have entered wrong choice\n");
}
getch();
}
}
void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting array elements are:- \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
                              }
void binary_search(int a[],int n,int element)
{
int beg,end,mid;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(element==a[mid])
{
printf("Element found at location %d\n",mid+1);
return;
}
else if(element>a[mid])
beg=mid+1;
else
end=mid-1;
}
printf("Element not found\n");
return;
                              }


Input/Output:



No comments:

Post a Comment