#include <iostream>
#include <vector>
using namespace std;
// Бульбашкове сортування (Bubble Sort)
void bubbleSort(vector<int> arr) {
int comparisons = 0, swaps = 0;
int n = arr.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
comparisons++;
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swaps++;
}
}
}
cout << "\n Бульбашкове сортування:\nВідсортований масив: ";
for (int num : arr) cout << num << " ";
cout << "\nКількість порівнянь: " << comparisons;
cout << "\nКількість перестановок: " << swaps << "\n";
}
// Сортування вставками (Insertion Sort)
void insertionSort(vector<int> arr) {
int comparisons = 0, swaps = 0;
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0) {
comparisons++;
if (arr[j] > key) {
arr[j + 1] = arr[j];
swaps++;
j--;
} else {
break;
}
}
arr[j + 1] = key;
}
cout << "\nСортування вставками:\nВідсортований масив: ";
for (int num : arr) cout << num << " ";
cout << "\nКількість порівнянь: " << comparisons;
cout << "\nКількість перестановок: " << swaps << "\n";
}
int main() {
vector<int> arr;
int n, value;
cout << "Введіть кількість елементів: ";
cin >> n;
cout << "Введіть елементи масиву:\n";
for (int i = 0; i < n; i++) {
cin >> value;
arr.push_back(value);
}
bubbleSort(arr);
insertionSort(arr);
return 0;
}