#include <iostream>
using namespace std;
void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
void insertionSort(int array[], int size) {
for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
while (key < array[j] && j >= 0) {
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
int main() {
int data[] = {9, 5, 1, 4, 3};
int size = sizeof(data) / sizeof(data[0]);
insertionSort(data, size);
cout << "Sorted array in ascending order:\n";
printArray(data, size);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCnZvaWQgcHJpbnRBcnJheShpbnQgYXJyYXlbXSwgaW50IHNpemUpIHsKICBmb3IgKGludCBpID0gMDsgaSA8IHNpemU7IGkrKykgewogICAgY291dCA8PCBhcnJheVtpXSA8PCAiICI7CiAgfQogIGNvdXQgPDwgZW5kbDsKfQoKdm9pZCBpbnNlcnRpb25Tb3J0KGludCBhcnJheVtdLCBpbnQgc2l6ZSkgewogIGZvciAoaW50IHN0ZXAgPSAxOyBzdGVwIDwgc2l6ZTsgc3RlcCsrKSB7CiAgICBpbnQga2V5ID0gYXJyYXlbc3RlcF07CiAgICBpbnQgaiA9IHN0ZXAgLSAxOwogICAgd2hpbGUgKGtleSA8IGFycmF5W2pdICYmIGogPj0gMCkgewogICAgICBhcnJheVtqICsgMV0gPSBhcnJheVtqXTsKICAgICAgLS1qOwogICAgfQogICAgYXJyYXlbaiArIDFdID0ga2V5OwogIH0KfQoKCmludCBtYWluKCkgewogIGludCBkYXRhW10gPSB7OSwgNSwgMSwgNCwgM307CiAgaW50IHNpemUgPSBzaXplb2YoZGF0YSkgLyBzaXplb2YoZGF0YVswXSk7CiAgaW5zZXJ0aW9uU29ydChkYXRhLCBzaXplKTsKICBjb3V0IDw8ICJTb3J0ZWQgYXJyYXkgaW4gYXNjZW5kaW5nIG9yZGVyOlxuIjsKICBwcmludEFycmF5KGRhdGEsIHNpemUpOwp9