Category: C++ Examples

C++ program to implement Binary Search

This tutorial explains how Binary Search works to find an element’s position in a sorted array. Besides this, you will come to know how to create a simple C++ program to implement binary search. #include<iostream> using namespace std; int binarysearch(int *a,int key,int n); int main( ) { int *arr,n,key; cout<<“Enter how many elements :”; cin>>n; […]

C++ program to implement Linear Search

#include<iostream> using namespace std; int linearsearch(int *array,int key,int n); int main() { int *arr,n,key; cout<<“Enter how many elements :”; cin>>n; arr=new int[n]; cout<<endl<<“Enter elements : “; for(int i=0;i<n;i++) cin>>arr[i]; cout<<endl<<“Enter the element to search : “; cin>>key; int r=linearsearch(arr,key,n); if(r) cout<<endl<<“element found at”<< r+1<<“position”; else cout<<endl<<“Element not found”; return 0; } int linearsearch(int *arr,int key,int […]

C++ program to implement Insertion Sort

#include<iostream> using namespace std; void Insertionsort(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]; Insertionsort(a,n); for(i=0;i<n;i++) cout<<endl<<a[i]; return 0; }; void Insertionsort(int *arr,int n) { int i, j, key; for(j = 1; j < n; j++) { key = arr[j]; for(i = j – […]

C++ program to implement Insertion Sort

#include<iostream> using namespace std; void Insertionsort(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]; Insertionsort(a,n); for(i=0;i<n;i++) cout<<endl<<a[i]; return 0; }; void Insertionsort(int *arr,int n) { int i, j, key; for(j = 1; j < n; j++) { key = arr[j]; for(i = j – […]

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