#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {4, 2, 1, 5, 3};
for (int i = 1; i < arr.size(); i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
for (int i = 0; i < arr.size(); i++) {
std::cout << arr[i] << " ";
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {4, 2, 1, 5, 3};
for (int i = 1; i < arr.size(); i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
for (int i = 0; i < arr.size(); i++) {
std::cout << arr[i] << " ";
}
return 0;
}