Program No 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

PROGRAM NO 1

LINEAR SEARCH #include<iostream.h> #include<conio.h>

int lsearch(int Arr[], int s, int VAL);

int main() { int Arr[100],n,val,found; cout<<"Enter number of elements you want to insert "; cin>>n; for(int i=0;i<n;i++) { cout<<"Enter element "<<i+1<<":"; cin>>Arr[i]; }

cout<<"Enter the number you want to search "; cin>>val;

found=lsearch(Arr,n,val);

if(found==1) cout<<"\nItem found"; else cout<<"\nItem not found";

getch(); return 0; }

int lsearch(int Arr[], int s, int VAL) { for(int I=0; I<s; I++) {

if(Arr[I]==VAL) return 1; } return 0; }

OUTPUT

PROGRAM NO 2
BINARY SEARCH #include<iostream.h> #include<conio.h>

int bsearch(int AR[], int N, int VAL);

int main() { int AR[100],n,val,found; cout<<"Enter number of elements you want to insert "; cin>>n; cout<<"Enter element in ascending order\n"; for(int i=0;i<n;i++) { cout<<"Enter element "<<i+1<<":"; cin>>AR[i]; }

cout<<"\nEnter the number you want to search "; cin>>val;

found=bsearch(AR,n,val);

if(found==1) cout<<"\nItem found"; else cout<<"\nItem not found";

getch(); return 0; }

int bsearch(int AR[], int N, int VAL) { int Mid,Lbound=0,Ubound=N-1;

while(Lbound<=Ubound) { Mid=(Lbound+Ubound)/2; if(VAL>AR[Mid]) Lbound=Mid+1; else if(VAL<AR[Mid]) Ubound=Mid-1; else return 1; }

return 0; }

OUTPUT

PROGRAM NO 3 INSERTION AND DELETION IN AN ARRAY


#include<iostream.h> #include<stdio.h> #include<conio.h>

main() { int array[15]; int no_el; clrscr(); cout<<"Enter the no of element :"; cin>>no_el; for(int i=0;i<no_el;i++) { cout<<"Enter the element : "; cin>>array[i]; }

while(1) { clrscr(); cout<<endl<<"1. Insert"; cout<<endl<<"2. delete by value"; cout<<endl<<"3. display"; cout<<endl<<"4. exit"; cout<<endl<<"Enter your choice : "; int choice; cin>>choice; switch(choice) { case 1: cout<<"Enter the position at which you want to insert : "; int pos; cin>>pos; cout<<"Enter the new element : "; cin>>new_el;

pos--; for(i=no_el-1;i>=pos;i--) array[i+1]=array[i]; array[pos]=new_el; no_el++; break; case 2: cout<<"Enter the value to be search : "; int key; cin>>key; for(pos=0;pos<no_el;pos++) { if(array[pos]==key) break; } if(pos==no_el) { cout<<"Search key not found";

break; } for(i=pos;i<no_el;i++) array[i]=array[i+1]; no_el--; break; case 3: cout<<endl; for(i=0;i<no_el;i++) cout<<endl<<"The element is : "<<array[i]; break; case 4: return(0); break; } getch(); } }

OUTPUT

You might also like