# Insertion Sort

/*********************************************************

-> This C++ program is to perform insertion sort.

-> This program works in microsoft vc++ 6.0 environment.

-> The numbers are sorted in increasing order.

**********************************************************/

#include<iostream.h>

class sorting
{
private:
int n;
double *array;
public:
void input();
void insertionsort();
void output();
};

void sorting::input()
{
cout<<“****************************************************\n”
<<“This program sorts numbers in increasing order”
<<“\n\t\tusing insertion sort technique\n”
<<“****************************************************\n”;

cout<<“Enter how many numbers you are going to enter for sorting ::”;
cin>>n;
array=new double[n+1];
for(int i=1;i<=n;i++)
cin>>array[i];
}

void sorting::insertionsort()
{
for(int i=1;i<=n;i++)
{
double x=array[i];
for(int j=i-1;j>0&&x<array[j];j–)
array[j+1]=array[j];
array[j+1]=x;
}
}
void sorting::output()
{
cout<<“Now the sorted numbers are ::\n”;
for(int i=1;i<=n;i++)
cout<<array[i]<<‘\t’;
cout<<endl;
}

int main()
{
sorting obj;
obj.input();
obj.insertionsort();
obj.output();
return 0;
}

/*******************************************************************

SAMPLE OUTPUT ::

****************************************************
This program sorts numbers in increasing order
using insertion sort technique
****************************************************
Enter how many numbers you are going to enter for sorting ::7
1.7
1.6
1.5
1.4
1.3
1.2
1.1
Now the sorted numbers are ::
1.1     1.2     1.3     1.4     1.5     1.6     1.7
Press any key to continue

********************************************************************/

## 5 thoughts on “Insertion Sort”

1. Thanks for sharing this information. Really is pack with new knowledge. Keep them coming.

2. #include
#include

int main()
{
int num[50],N,i,j,key;
cout <> N;
cout << "\nEnter " << N << " Numbers\n";
for(int i = 0;i > num[i];

for(j = 1;j = 0 && num[i] > key)
{
//Swap two elements
num[i + 1] = num[i];
i–;
num[i + 1] = key;
}
}

//Print Output
cout << endl;
for(i = 0;i < N;i++)
cout << num[i] << ' ';

getch();
return 0;
}

3. Great blog here! Also your site loads up fast! What web host are you
quickly as yours lol

4. I don’t even know how I ended up here, but I thought this post was good. I don’t know
who you are but definitely you are going to a famous blogger if you aren’t already 😉 Cheers!