Skip to main content

Posts

Showing posts from October, 2018

Quick Sort

Quick Sort : The Quick sort Algorithm is the most popular and widely used sorting algorithm developed by C.A.R Hoare in 1992. This algorithm tends be the quickest in the average case among all the sorting technique. It works on divide and conquer strategy. In divide and conquer policy the the problem is divided into smaller subproblems repeatedly until we reach the smaller size subproblems whose solution is easy to find. #include<iostream> using namespace std; template <class T> int partition(T a[], int start, int end) { T pivot = a[end]; int pindex = start; for(int i=start;i<end;i++) { if(a[i]<=pivot) { swap(a[i],a[pindex]); pindex++; } } swap(a[pindex],a[end]); return pindex; } template<class T> int quicksort(T a[], int start, int end) { if(start<end) { int pindex = partition(a,start,end); quicksort(a,start,pindex-1); quicksort(a,pindex+1,end); } } template <class T> void read(T a[], i...

Insertion Sort In C++

Insertion Sort Program in C++ Insertion Sort:    In this Method, the element is inserted at its proper position in the sorted list. In this method initially the whole list is divided into two parts,one part containing the first element and the second containing the other elements. The first Part is called the sorted list and the second part is called the unsorted list. In other words we have to divide the list into two parts. sorted list and unsorted list. We start with the first element of unsorted list (the second element of the list) and insert it into its proper position in the sorted list. #include<iostream> using namespace std; template <class T> void read(T a[], int n) {     for(int i=0;i<n;i++) {         cout<<"Enter elements:";         cin>>a[i];     } } template <class T> void display(T a[], int n) {     for(int i=0;i<n;i++) {   ...

Selection Sort In C++

SELECTION SORT Selection Sort: In Slection Sort we need to search for the smallest element in the list and interchange it with the element in the first place (0th) position. And we need to search for the smallest element in the list and interchange it with element in the second (1st) potion. #include<iostream> using namespace std; template <class T> void read(T a[], int n) {     for(int i=0;i<n;i++) {         cout<<"Enter elements:";         cin>>a[i];     } } template <class T> void display(T a[], int n) {     for(int i=0;i<n;i++) {         cout<<" "<<a[i];     } } template <class T> void sort(T a[], int n) {     for(int i=0;i<n;i++) {         for(int j=i+1;j<n;j++) {             if(a[j]<a[i]) {  ...