Professional Documents
Culture Documents
4. BUBBLE SORT
6. INSERTION SORT
7. PRODUCT OF MATRICES
#include<iostream.h>
#include<conio.h>
int bsearch(int[],int,int);
void main()
{
clrscr();
int ar[50],item,n,index;
clrscr();
cout<<"enter array size(max.50):";
cin>>n;
cout<<"\n array elements sorted in asc. order :";
for(int i=0;i<n;i++)
{ cin>>ar[i];}
cout<<"\nenter elements to be searched for:";
cin>>item;
index=bsearch(ar,n,item);
if(index==-1)
cout<<"\nsorry!! element could not be found\n";
else
cout<<"\n element found at index:" <<index<<",position:"<< index+1 <<endl;
getch();
}
int bsearch(int ar[],int size,int item)
{ int beg,last,mid;
beg=0; last=size-1;
while(beg<=last)
{mid=(beg+last)/2;
if(item==ar[mid])
return mid;
else if(item>ar[mid])
beg=mid+1;
else
last=mid-1;
}
return-1;
}
/***********************PROGRAM #3********************/
/*************DELETE AN ELEMENT FROM AN ARRAY*********/
#include<iostream.h>
#include<conio.h>
#include<process.h> //for exit()
int lsearch(int[],int,int);
//function to determine the right position for new element
void main()
{
clrscr();
int ar[50],item,n,index; //array can hold ,ax. 50 elements
//clrscr();
cout<<"how many elements do u want to create array with?(max 50_..)";
cin>>n;
cout<<"\n enter array elements\n";
for(int i=0;i<n;i++)
{
cin>>ar[i];}
char ch='y';
while(ch=='y'|| ch=='Y')
{cout<<"\nenter elements to be deleted";
cin>>item;
if(n==0)
{
cout<<"underflow!!\n";
exit(1);
}
index=lsearch(ar,n,item);
if(index!=-1)
ar[index]=0;
else
{
cout<<"sorry!! No such element in the array\n";
exit(1);
}
cout<<"the array now is as shown below\n";
cout<<"zero(0) signifies deleted element\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
cout<<"after this emptied space will be shifted to the end of array\n";
for(i=index;i<n;i++)
{
ar[i]=ar[i+1];
}
n-=1;
cout<<"\n want to delete more ?(y/n)\n";
cin>>ch;}
cout<<"the array after shifting is:";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
}