2020-05-27 01:02:10 +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
|
|
|
|
2020-05-27 01:02:10 +08:00
|
|
|
int main() {
|
|
|
|
int n;
|
|
|
|
std::cout << "\nEnter the length of your array : ";
|
|
|
|
std::cin >> n;
|
|
|
|
int *Array = new int[n];
|
|
|
|
std::cout << "\nEnter any " << n << " Numbers for Unsorted Array : ";
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2020-05-27 01:02:10 +08:00
|
|
|
// Input
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::cin >> Array[i];
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2020-05-27 01:02:10 +08:00
|
|
|
// Sorting
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
int temp = Array[i];
|
|
|
|
int j = i - 1;
|
|
|
|
while (j >= 0 && temp < Array[j]) {
|
|
|
|
Array[j + 1] = Array[j];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
Array[j + 1] = temp;
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2020-05-27 01:02:10 +08:00
|
|
|
// Output
|
|
|
|
std::cout << "\nSorted Array : ";
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::cout << Array[i] << "\t";
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] Array;
|
|
|
|
return 0;
|
2016-07-19 15:02:14 +08:00
|
|
|
}
|