Program to sort an array of integers in ascending order using insertion sort.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int a[16], i, j, k, temp;
    cout<<"Keep Entering the elements\n";
   for (i = 0; i < 16; i++)
   {
       cin>>a[i];
   }
   for (i = 1; i < 16; i++)
   {
       for (j = i; j >= 1; j--)
       {
           if (a[j] < a[j-1])
           {
               temp = a[j];
               a[j] = a[j-1];
               a[j-1] = temp;
           }
           else
               break;
       }
}
    cout<<"sorted array\n"<<endl;
   for (k = 0; k < 16; k++)
   {
cout<<a[k]<<endl;
   }
cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";  
    getch();
}

Output:



-Thanks Sachin Bagga Sir

Comments

Popular posts from this blog

To implement the various components of HTML5 Canvas

Program to illustrate the concept of templates.

Program to illustrate the order of execution of constructors and destructors in inheritance.