Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 38

Yashika Sharma 90680308261 1.

WAP to find

the factorial of a number using function and its time

complexity.
Program: #include<iostream.h> #include<conio.h> #include<time.h> #include<dos.h> long factfn(int f) { long fact=1; for(int i=f;i>0;i--) fact=fact*i; return fact; } void main() { clrscr(); long n,factorial; clock_t start,end; cout<<"Enter the no. to find its factorial using function\n"; cin>>n; start=clock(); factorial=factfn(n); cout<<"\nFactorial of the no. "<<n<<" is "<<factorial; delay(100); end=clock(); cout<<"\nTotal time consumed is "<<(end-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

Yashika Sharma 90680308261

2.WAP to find the factorial of a number by recursion and its time complexity.
#include<iostream.h> #include<conio.h> #include<dos.h> #include<time.h> int fact(int num, int fac) { if(num==1) { return fac; } fac=fac*num; fact(num-1,fac); } void main() { clrscr(); int num,fac=1; clock_t start,end; cout<<"\n Enter the number "; cin>>num; start=clock(); fac=fact(num,fac); cout<<"\n Factorial is "<<fac; delay(100); end=clock(); cout<<"\n Time elapsed is "<<(end-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

Yashika Sharma 90680308261

3.Write to find fibbonaci series by recursion and its time complexity.


Program: #include<iostream.h> #include<conio.h> #include<dos.h> #include<time.h> void main() { int fibbo(int); int i; clrscr(); clock_t start,end; start=clock(); for(i=1;i<8;i++) cout<<fibbo(i)<<endl; delay(100); end=clock(); cout<<"\n Time elapsed is"<<(end-start)/CLK_TCK; getch(); } int fibbo(int i) { if(i==1) { return(0); } else if(i==2) { return(1);

Yashika Sharma 90680308261

} else { return(fibbo(i-2)+fibbo(i-1)); } }

Yashika Sharma 90680308261

4.Write to find fibbonaci series by function and its time complexity.


Program: #include<iostream.h> #include<conio.h> #include<time.h> #include<dos.h> void main() { clrscr(); void fibbo(int); int n; clock_t start,end; start=clock(); cout<<"enter the number of terms you want \t"; cin>>n; fibbo(n); delay(100); end=clock(); cout<<"\n Total time elapsed is "<<(end-start)/CLK_TCK; getch(); } void fibbo(int num) { int a=1; int b=1; int c; cout<<a<<"\t"<<b; for(int i=2;i<num;i++) {

Yashika Sharma 90680308261

c=a+b; cout<<"\t"<<c; a=b; b=c; } }

Yashika Sharma 90680308261

5. WAP in c++ to implement stack using array. Program


#include<iostream.h> #include<conio.h> #include<process.h> #include<time.h> #include<dos.h> int push(int[],int&,int); void display(int[],int); const int size=50; void main() { int stack[10],item,top=-1,res; char ch='y'; clrscr(); while(ch=='y'||ch=='Y') { clock_t start,end; start=clock(); cout<<"\nenter item for insertion = "; cin>>item; delay(100); end=clock(); cout<<"\ntotal time consumed is"<<(end-start)/CLK_TCK; res=push(stack,top,item); if(res==-1) {

Yashika Sharma 90680308261

cout<<"overflow"; exit(1); } cout<<"\nthe stack is now: \n"; display(stack,top); cout<<"want to insert more elements(y/n) "; cin>>ch; } } int push(int stack[],int & top,int ele) { if(top==size-1) return-1; else { top++; stack[top]=ele; } return 0; } void display(int stack[],int top) { cout<<stack[top]<<endl; for(int i=top-1;i>=0;i--) cout<<stack[i]<<endl; }

Yashika Sharma 90680308261

Yashika Sharma 90680308261

6.WAP IN C++ to implement circular Queues.


Program: #include<iostream.h> #include<conio.h> #include<process.h> #include<time.h> #include<dos.h> #define SIZE 8 class queue { int elem[6]; int front,rear; public: queue() { front=rear=-1; } void insert(int); void remove(); void display(); ~queue() { cout<<"destroying simple queue"; } }; int main() { clrscr(); queue q1; int choice,val; char ch; while(1)

Yashika Sharma 90680308261

{ clock_t start,end; start=clock(); cout<<"\nchoice 1.insert\t 2.remove\t 3.display\t 4.exit= "; cin>>choice; switch(choice) { case 1:cout<<"\nenter element to push= "; cin>>val; delay(100); end=clock(); cout<<"\ntotal time consumed is"<<(end-start)/CLK_TCK; q1.insert(val); break; case 2:q1.remove(); delay(100); end=clock(); cout<<"\ntotal time consumed is"<<(end-start)/CLK_TCK; break; case 3:q1.display(); delay(100); end=clock(); cout<<"\ntotal time consumed is"<<(end-start)/CLK_TCK; break; case 4:exit(0); } } } void queue::insert (int item) { if((rear==(SIZE-1)&& front==0)||(front==rear+1)) cout<<"overflow queue is full"; else if(rear==-1)

Yashika Sharma 90680308261

{ front=rear=0; elem[rear]=item; } else if(rear==(SIZE-1)) { rear=0; elem[rear]=item; } else { rear++; elem[rear]=item; } } void queue::remove() { if(front==-1) cout<<"underflow"; else if(rear==front) rear=front=-1; else if(front==(SIZE-1)) front=0; else front++; } void queue::display() { int i; if(rear>=front) { for(i=front;i<=rear;i++) cout<<elem[i]<<'\t';

Yashika Sharma 90680308261

} else { for(i=front;i<=(SIZE-1);i++) cout<<elem[i]<<'\t'; for(i=0;i<=rear;i++) cout<<elem[i]<<'\t'; } }

Yashika Sharma 90680308261

7. WAP to sort the elements using insertion sort and find its time Complexity.
PROGRAM #include<iostream.h> #include<conio.h> #include<dos.h> #include<time.h> void insertion(int a[], int n) { int ptr; for(int k=0;k<n;k++) { int temp=a[k]; ptr=k-1; while( (temp<a[ptr]) && (ptr>=0)) { a[ptr+1]=a[ptr]; ptr=ptr-1; } a[ptr+1]=temp; } } void main() { clrscr(); int n,index; clock_t start,end; cout<<"\n Enter the no. of elements: ";

Yashika Sharma 90680308261

cin>>n; int *a = new int[n]; cout<<"\n Enter the elements: "; for(int i=0;i<n;i++) { cin>>a[i]; } start=clock(); insertion(a,n); delay(100); end=clock(); cout<<"\n Sorted list is; for(i=0;i<n;i++) { cout<<"\n"<<a[i]; } cout<<"\n Time elasped is: "<<(end-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

8. WAP to sort the elements using selection sort and find its time complexity.
Program: #include<iostream.h> #include<conio.h> #include<dos.h> #include<time.h> void selection(int a[], int n) { int min,loc1; for(int i=0;i<n-1;i++) { min=a[i]; loc1=i; for(int j=i+1;j<n;j++) { if(min>a[j]) { min=a[j]; loc1=j; } } int temp=a[i]; a[i]=a[loc1]; a[loc1]=temp; } } void main()

Yashika Sharma 90680308261

{ clrscr(); int n,index; clock_t start; cout<<"\n Enter the no. of elements: "; cin>>n; int *a = new int[n]; cout<<"\n Enter the elements: "; for(int i=0;i<n;i++) { cin>>a[i]; } start=clock(); selection(a,n); delay(100); end=clock(); cout<<"\n Sorted list is: "; for(i=0;i<n;i++) { cout<<"\n"<<a[i]; } cout<<"\n Time elasped is: "<<(end-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

9. WAP to find the element using binary search and its time complexity.
Program: #include<iostream.h> #include<conio.h> #include<time.h> #include<dos.h> void sort(int tmp_arr[], int tmp_size) { for(int i=0;i<tmp_size-1;i++) { for(int j=0;j<tmp_size-1;j++) { int tmp; if (tmp_arr[j]>tmp_arr[j+1]) { tmp=tmp_arr[j]; tmp_arr[j]=tmp_arr[j+1] tmp_arr[j+1]=tmp; } } }

Yashika Sharma 90680308261

} void search(int array[], int item, int sz,int st) { int mid,beg,end; beg=st; end=sz; mid=(beg+end)/2; if (item==array[mid]) { cout<<"\n No.is found"; } else if(item<array[mid]) { search(array,item,mid-1,beg); } else if (item>array[mid] { search(array,item,end,mid+1); } else { cout<<"\n Not found"; } } void main() { int beg,end,mid,size,item; clrscr(); clock_t start, endclk; cout<<"\n Enter the number of elements: "; cin>>size;

Yashika Sharma 90680308261

int *arr=new int[size]; cout<<"\n Enter elements: "; for(int i=0;i<size;i++) { cin>>arr[i]; } start=clock(); sort(arr,size); cout<<"\n Enter the no. to search cin>>item; search(arr,item,size,1); delay(100); endclk=clock(); cout<<"\n Time elapsed is: "<<(endclk-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

10. WAP to find the element using linear search and its time complexity.
Program: #include<iostream.h> #include<conio.h> #include<time.h> #include<dos.h> void main() { clrscr(); int i,n,item; clock_t start, end; cout<<"\n Enter the size of an array: "; cin>>n; int *a= new int[n]; cout<<"\n Enter the elements of an array: "; for(i=0;i<n;i++) { cin>>a[i]; }

Yashika Sharma 90680308261

cout<<"\n Enter the element you want to search: "; cin>>item;

start=clock(); for(i=0;i<n;i++) { if(a[i]==item) { cout<<"\n Position of element is: "<<i+1; } } delay(100); end=clock(); cout<<"\n Time elapsed is: "<<(end-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

11.WAP to find Maxima and Minima and its time complexity.


Program: #include<iostream.h> #include<conio.h> #include<time.h> #include<dos.h> void main() { clrscr(); int n,i,x=0; cout<<"\n Enter no. of elements: "; cin>>n; int *a= new int[n]; cout<<"\n Enter the elements: "; for(i=1;i<=n;i++) {

Yashika Sharma 90680308261

cin>>a[i]; } for(i=1;i<=n;i++) { if (x<a[i]) { x=a[i]; } } cout<<"\n Maximum no.is: "<<x; for(i=1;i<=n;i++) { if (x>a[i]) { x=a[i]; } } cout<<"\n Minimum no.is: "<<x; getch(); }

Yashika Sharma 90680308261

12. WAP to sort the elements using quick sort and find its time complexity.
Program: #include<iostream.h> #include<conio.h> #include<dos.h> #include<time.h> void quicksort(int a[], int low, int high) { int i = low; int j = high; int y = 0; int z = a[(low + high) / 2]; do { while(a[i] < z) i++;

Yashika Sharma 90680308261

while(a[j] > z) j--; if(i <= j) { y = a[i]; a[i] = a[j]; a[j] = y; i++; j--; } } while(i <= j); if(low < j) quicksort(a, low, j); if(i < high) quicksort(a, i, high); } void main() { int n,index; clock_t start,end; cout<<"\n Enter the no. of elements: "; cin>>n; int *a = new int[n]; cout<<"\n Enter the elements: "; for(int i=0;i<n;i++) cin>>a[i]; start=clock(); quicksort(a,0,(n-1)); delay(100); end=clock();

Yashika Sharma 90680308261

cout<<"\n Sorted list is: "; for(i=0;i<n;i++) cout<<"\n"<<a[i]; cout<<"\n Time elasped is: "<<(end-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

13. WAP to sort the elements using merge sort and find its time complexity.
Program:

#include<iostream.h> #include<conio.h> #include<time.h> #include<dos.h> int n=0; void Merge(int low, int mid, int high, int arr[]) { int h,i,j; int *b= new int[n]; h=low; i=low; j=mid+1; while((h<=mid)&&(j<=high)) { if (arr[h]<=arr[j]) { b[i]=arr[h]; h=h+1; } else

Yashika Sharma 90680308261

{ b[i]=arr[j]; j=j+1; } i=i+1; } if (h>mid) { for(int k=j; k<=high; k++) { b[i]=arr[k]; i=i+1; } } else { for(int k=h; k<=mid; k++) { b[i]=arr[k]; i=i+1; } } for (int k=low;k<=high;k++) arr[k]=b[k]; } void MergeSort(int low, int high, int arr[]) { int mid; if (low<high) { mid=(low+high)/2;

Yashika Sharma 90680308261

MergeSort(low,mid,arr); MergeSort(mid+1,high,arr); Merge(low, mid, high,arr); } } void main() { int low, high,i; clock_t start, end; clrscr(); cout<<"\n Enter no. of elements: "; cin>>n; int *arr= new int[n]; cout<<"\n Enter the elements: "; for(i=1;i<=n;i++) cin>>arr[i]; start=clock(); low=1; high=n; MergeSort(low, high,arr); cout<<"\n Sorted list is: "; for(i=1;i<=n;i++) cout<<arr[i]<<endl; delay(100); end=clock(); cout<<"\n Time elapsed is: "<<(end-start)/CLK_TCK; getch(); }

Yashika Sharma 90680308261

14. WAP in C++ to implement stack using linked list.


Program: #include<iostream.h> #include<conio.h> #include<process.h> struct node { int info; node*next; } *top,*newptr,*save,*ptr; node*create_new_node(int); void push(node*); void display(node*); void main() { int inf; char ch='y';

Yashika Sharma 90680308261

top=NULL; clrscr(); while(ch=='y' || ch=='Y') { clock_t start,end; start=clock(); cout<<"enter info of new node"; cin>>inf; delay(100); end=clock(); cout<<"\ntotal time consumed is"<<(end-start)/CLK_TCK; newptr=create_new_node(inf); if(newptr==NULL) { cout<<"cannot create new node "; exit(1); } push(newptr); cout<<"now stack is = "; display(top); cout<<"do you want to continue(y/n) "; cin>>ch; } } node*create_new_node(int n) { ptr=new node; ptr->info=n; ptr->next=NULL; return ptr; } void push(node*np)

Yashika Sharma 90680308261

{ if(top==NULL) top=np; else { save=top; top=np; np->next=save; } } void display(node*np) { while(np!=NULL) { cout<<np->info<<" "; np=np->next; } cout<<"\n"; }

Yashika Sharma 90680308261

15.Wap in C++ to implement a program for heap sort.

#include<iostream.h> #include<conio.h> void insert (int a[],int n) { Int i=n,item=a[n]; While(i>1)&&(a[i/2)]<item)) { a[i]=a[(i/2)]; i=(i/2); } a[i]=item; } Void adjust(int a[],int I,int n); { Int j=(2*i); Int item=a[i]; While(j<=n) { If(j<n)&&(a[j]<a[j+1])) J=j+1;

Yashika Sharma 90680308261

If(item>=a[j]) break; a[(j/2]=a[j]; j=(2*j); } Int delmax(int a[],int n,int x) { If(n==0) Cout<<\n heap is empty; X=a[i]; a[i]=a[n]; adjust(a,1,n-1); return x; } Void heapsort(int a[],int n) { Int x; for (int i=1;i<=n,i++) insert(a,i) for(i=n;i>=1;i--) { a[i]=delmax(a,i,x); } } void main() { Clrscr(); int *a,n; cout<<\n the number of elements are:\n; cin>>n; cout<<enter the elements; for(i=1;i<=n;i++) cin>>a[i]; heapsort(a,n); cout<<\n the sorted elements are:\n; for(i=1;i<=n;i++) cout<<a[i]<<\t; getch(); } Output: The number of elements to be sorted 5 Enter the elements 25

Yashika Sharma 90680308261

27 30 18 19 The sorted elements are : 18 19 25

27

30

You might also like