TheAlgorithms-C-Plus-Plus/Insertion Sort.cpp

39 lines
471 B
C++
Raw Normal View History

2016-08-30 17:31:33 +08:00
//Insertion Sort
2016-07-19 15:02:14 +08:00
#include<iostream>
using namespace std;
int main()
{
2016-08-30 17:31:33 +08:00
int n;
int Array[n];
2016-07-19 15:02:14 +08:00
cout<<"\nEnter any 6 Numbers for Unsorted Array : ";
//Input
2016-08-30 17:31:33 +08:00
for(int i=0; i<n; i++)
2016-07-19 15:02:14 +08:00
{
cin>>Array[i];
}
//Sorting
2016-08-30 17:31:33 +08:00
for(int i=1; i<n; i++)
2016-07-19 15:02:14 +08:00
{
int temp=Array[i];
int j=i-1;
while(j>=0 && temp<Array[j])
{
Array[j+1]=Array[j];
j--;
}
Array[j+1]=temp;
}
//Output
cout<<"\nSorted Array : ";
2016-08-30 17:31:33 +08:00
for(int i=0; i<n; i++)
2016-07-19 15:02:14 +08:00
{
cout<<Array[i]<<"\t";
}
}