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