Write a Program in C++ for Insertion Sort Algorithm

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

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment