Tuesday, 5 January 2016

Write a Program in c++ for insertion sort algorithm

Write a Program in c++ for insertion sort algorithm

 Cplusplus, insertion sort algorithm in c, what is insertion sort algorithm.

Structure of the Problem Requirements  
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quick sort, heap sort, or merge sort.
Source Code 
#include <iostream>
using namespace std;

int main()
{
cout<<"\n\n\t *************** INSERATION SORT ALGORITHM *****************\n\n\n";
 int A[] = {31,41,59,26,42,58};
 for(int j = 1,key,i; j<6; j++)
 {
  key = A[j];
  i = j-1;
  while(i>=0 and A[i]>key)
  {
   A[i+1] = A[i];
   i = i-1;
  }
  A[i+1] = key;
 }

 for(int k=0; k<6; k++)
 {
  cout<<A[k]<<" "<<endl;
 }
 cout<<endl;
 system("PAUSE");
 return 0;
}
Output of the Program
 insertion sort algorithm


No comments:

Post a Comment