/*-------------------------------------------------------------------------------------------------------------
File Name: insertion.cpp
Author: sujith
Program: Write a C++ program to implement
insertion Sort
Date: 23-04-2015
--------------------------------------------------------------------------------------------------------------*/
#include<iostream>
#include<stdio.h>
using
namespace std;
void
insertionsort(int a[],int n);
int
main()
{
int i,n,a[20];
cout<<"Enter the total
number of element\n";
cin>>n;
cout<<"Enter the
Elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"After
sorting\n";
insertionsort(a,n);
for(i=0;i<n;i++){
cout<<a[i]<<"\n";
}
}
void
insertionsort(int a[],int n)
{
int i,j,key;
for(i=1;i<n;i++)
{
key=a[i];
for(j=i-1;j>=0&&key<a[j];j--){
a[j+1]=a[j];
}
a[j+1]=key;
}
}
OUTPUT:
[141740@localhost
~]$ g++ insertion.cpp
[141740@localhost
~]$ ./a.out
Enter
the total number of element
5
Enter
the Elements
5 6 3 7 9
After
sorting
3
5
6
7
9
[141740@localhost
~]$ g++ insertion.cpp
[141740@localhost
~]$ ./a.out
Enter
the total number of element
5
Enter
the Elements
56 8 2 7 4
After
sorting
2
4
7
8
56
[141740@localhost
~]$
No comments:
Post a Comment