Insertion Sorting
C
++ program for insertion sort to sort numbers. This code implements insertion sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[10],i,j,n,key;
cout<<"\Program for Insertion Sort by-Tarun Rawat\n";
cout<<"\nEnter Total Elements: "; cin>>n;
cout<<"\nEnter elements: \n";
for(i=0;i<n;i++)
{cin>>a[i];
}
for(j=1;j<n;j++)
{key=a[j];
i=j-1;
while(i>=0 && a[i]>key)
{a[i+1]=a[i];
i=i-1;
} a[i+1]=key;
}
for(i=0;i<n;i++)
{cout<<a[i]<<" ";
}getch();
}
Find Us On Google, Just type - way2cplusplus -
If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)
No comments:
Post a Comment