You are on page 1of 5

PROGRAM CODE

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int Linear(int A[10],int n,int ele)
{
for(int i=0;i<n;i++)
{
if(A[i]==ele)
return i+1;
}
return -1;
}

int Binary(int A[10],int n,int ele)


{
int beg=0,end=n-1,mid;
while(beg<=end)
{
mid=int((beg+end)/2);
if(A[mid]==ele)
return mid+1;
else if(A[mid]>ele)
end=mid-1;
else if(A[mid]<ele)
beg=mid+1;
}
return -1;
}
void Bubble(int A[],int n)
{
int i,j;
cout<<"\n\n\t\t\tBubble Sorting";
cout<<"\n\n\t\t\tElements before Bubble Sort: ";
for(i=0;i<n;i++)
cout<<A[i]<< " ";
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(A[i]<A[j])
{
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
cout<<"\n\n\t\t\tElements after Bubble Sort: ";
for(i=0;i<n;i++)
{
cout<<A[i]<<" ";
}
}

void Selection(int A[10],int n)


{

int small,pos,i,j=0;
cout<<"\n\n\t\t\tElements before Exchange Selection sorting:";
for(i=0;i<n;i++)
cout<<A[i]<<" ";
for(i=0;i<n-1;i++)
{
small=A[i];
for(j=i+1;j<n;j++)
{
if(A[j]<small)
{
small=A[j];
pos=j;
}
}
int temp=A[i];
A[i]=A[pos];
A[pos]=temp;
}
cout<<"\n\n\t\t\tElements after Exchange Selection sorting:";
for(i=0;i<n;i++)
cout<<A[i]<<" ";
}

int findpos(int A[10],int n,int ele)


{
int i ;
int pos;
if(ele<A[0])//AT BEGINNING
pos=0;
else
{
for(i=0;i<n;i++)
if(ele>=A[i]&&ele<A[i+1])//AT MIDDLE POSITION
{
pos=i+1;
break;
}
}
if(i==n)//AT LAST
pos=n;
return pos;
}

void Insert(int A[10],int &n,int pos,int ele)


{
for(int i=n-1;i>=pos;i--)
A[i+1]=A[i];//SHIFTING ELEMENTS DOWNWARDS
n=n+1;
A[pos]=ele;
cout<<"\n\nElements after insertion";
for(i=0;i<n;i++)
cout<<A[i]<<" ";
}

void Del(int A[10],int &n,int pos)


{
for(int i=pos+1;i<n;i++)
A[i-1]=A[i];//SHIFTING ELEMENTS UPWARDS
n=n-1;
cout<<"\n\n\t\t\tElements after deletion: ";
for(i=0;i<n;i++)
cout<<A[i]<<" ";
}

void main()
{
cout<<"\t\t\t\Welcome to C++ ";
int A[100],n,i,choice,ele,X;
cout<<"\n\nEnter size of integer array:";
cin>>n;
cout<<"\n\nEnter elements in integer array:";
for(i=0;i<n;i++)
cin>>A[i];
cout<<"\n\t\t\tSelect operation to be performed:-\n\n1.Linear
Search\n\n2.Binary Search\n\n3.Bubble Sorting\n\n4.Exchange
Selection Sorting\n\n5.Insertion of element into array\n\n6.Delete an
element from array\n\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n\n\t\t\tLinear Search";
cout<<"\n\nEnter element to be searched:";
cin>>ele;
if(Linear(A,n,ele)==-1)
cout<<"\n\nElement not found";
else
cout<<"\n\nElement found at:"<<Linear(A,n,ele);
break;
case 2:cout<<"\n\n\t\t\tBinary Search";
cout<<"\n\nEnter element to be searched:";
cin>>ele;
if(Binary(A,n,ele)==-1)
cout<<"\n\nElement not found";
else
cout<<"\n\nElement found at:"<<Binary(A,n,ele);
break;
case 3:Bubble(A,n);
break;
case 4:Selection(A,n);
break;
case 5:cout<<"\n\n\t\t\tInsert Element into Array:";
cout<<"\n\nEnter element to be inserted:";
cin>>ele;
X=findpos(A,n,ele);
cout<<"\n\nElement inserted at:"<<X;
Insert(A,&n,X,ele);
break;
case 6:cout<<"\n\n\t\t\tDelete Element into Array:";
cout<<"\n\nEnter element to be deleted:";
cin>>ele;
X=findpos(A,n,ele);
Del(A,&n,X);
break;
default:cout<<"\nInvalid choice:";
}
}

You might also like