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 create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex