Blog

C++ program to implement QuickSort

#include<iostream> using namespace std; int partition(int left,int right,int arr[]); void quicksort(int left,int right,int arr[]); int main() { int *a,n,left,right,i; cout<<“Enter number of elements:”; cin>>n; a=new int[n]; for(i=0;i<n;i++) cin>>a[i]; right=n-1; left=0; quicksort(left,right,a); for(i=0;i<n;i++) cout<<a[i]<<” “; return 0; } int partition(int left,int right,int arr[]) { int i,high,low,pivot; pivot=arr[left]; while(right>left) { high=arr[right]; while(pivot<high) { if(right<=left) break; right–; high=arr[right]; […]

C++ program to implement Merge Sort

#include<iostream> using namespace std; void mergesort(int *,int ); void merge(int*,int,int*,int,int*,int); int main() { int *arr,n; cout<<“Enter the no of elements “; cin>>n; arr=new int[n]; for(int i=0;i<n;i++) cin>>arr[i]; mergesort(arr,n); for(i=0;i<n;i++) cout<<arr[i]<<” “; return 0; } void mergesort(int *arr,int n) { if(n>1) { int *b; b=new int[n/2]; for(int i=0;i<n/2;i++) b[i]=arr[i]; int *c; c=new int[n-n/2]; for(int j=0;i<n;i++,j++) c[j]=arr[i]; […]

C++ program to implement Heap Sort

#include<iostream> using namespace std; void heapsort(int *a,int n); void adjust(int *a,int root,int n); int main( ) { int *a,n; cout<<“Enter how many elements :”; cin>>n; a=new int[n+1]; for(int i=1;i<=n;i++) cin>>a[i]; heapsort(a,n); for(i=1;i<=n;i++) cout<<a[i]<<” “; return 0; } void heapsort(int *a,int n) { for(int i=n/2;i>=1;i–) adjust(a,i,n); for(i=n-1;i>=1;i–) { int t=a[i+1]; a[i+1]=a[1]; a[1]=t; adjust(a,1,i); } } void […]

C++ program to implement Bubble Sort

#include<iostream> using namespace std; void bubble(int *,int); int main() { int n; int arr[20]; cout<<endl<<“Enter how many elements :”; cin >> n; for(int i=0;i<n;i++) cin>>arr[i]; bubble(arr,n); for(i=0;i<n;i++) cout<<endl<<arr[i]; return 0; }; void bubble(int *a,int n) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(a[j]>a[j+1]) { int temp; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } Input Enter […]

C++ program to implement Selection Sort

#include<iostream> using namespace std; void sort(int *,int); int main( ) { int n; int a[20]; cout<<endl<<“Enter how many elements :”; cin >> n; for(int i=0;i<n;i++) cin>>a[i]; sort(a,n); for(i=0;i<n;i++) cout<<a[i]; return 0; }; void sort(int *a,int n) { for(int i=0;i<n;i++) { int j=i; for(int k=i+1;k<n;k++) { if(a[k]<a[j]) j=k; } int temp=a[i]; a[i]=a[j]; a[j]=temp; } } Input […]

C++ program to illustrate virtual functions

#include<iostream> using namespace std; class B { public: virtual void display() { cout<<endl<<“B’s show”; } }; class D : public B { public: void display() { cout<<endl<<“D’s show”; } }; int main() { B *ptr; B b; ptr=&b; ptr->display(); D d; ptr=&d; ptr->display(); return 0; } Output B’s show D’s show

C++ program to illustrate pointer to base class

#include<iostream> using namespace std; class Base { public: void show() { cout<<endl<<“Base show”; } }; class Derived : public Base { public: void show() { cout<<endl<<“Derived show”; } }; int main() { Base *ptr; Base b; ptr=&b; ptr->show(); Derived d; ptr=&d; ptr->show(); Derived *dptr; dptr=&d; dptr->show(); return 0; } Output Base show Base show Derived […]

C++ program to illustrate pointer to a function

#include<iostream> using namespace std; void (*ptr)(int); void change(int); void change(int a) { a++; cout<<endl<<a; } int main() { ptr=change; (*ptr)(10); return 0; } Output 11

C++ program to illustrate the use of array of pointers

#include<iostream> #include<string> using namespace std; int main() { char *ptr[10]; char temp[20]; int n,m; cout<<“Enter how many names :”; cin>>n; for(int i=0;i<n;i++) { cin>>temp; m=strlen(temp); ptr[i]=new char[m+1]; strcpy(ptr[i],temp); } for(i=0;i<n;i++) cout<<endl<<ptr[i]; for(i=0;i<n;i++) delete ptr[i]; return 0; } Input Enter how many names : 3 Sartaj Sahni Galvin Output Sartaj Sahni Galvin

C++ program to illustrate pointers

#include<iostream> using namespace std; int main() { int a=15; int *ptr; int **pptr; ptr=&a; pptr=&ptr; cout<< a << *ptr << **pptr; return 0; } Output 15  15  15