#include <iostream>
#include <vector>
using namespace std;
// Funkcja sortująca tablicę za pomocą sortowania bąbelkowego
void bubbleSort(vector<int>& arr) {
int n = arr.size();
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false; // Resetujemy flagę zamiany
for (int j = 0; j < n - i - 1; ++j) {
// Porównujemy sąsiednie elementy
if (arr[j] > arr[j + 1]) {
// Zamieniamy je miejscami
swap(arr[j], arr[j + 1]);
swapped = true; // Ustawiamy flagę zamiany
}
}
// Jeśli nie było zamiany, tablica jest już posortowana
if (!swapped) {
break;
}
}
}
// Funkcja do wypisywania tablicy
void printArray(const vector<int>& arr) {
for (int num : arr) {
cout << num << " ";
}
cout << endl;
}
int main() {
vector<int> arr;
int n, num;
cout << "Podaj liczbę elementów w tablicy: ";
cin >> n;
cout << "Podaj elementy tablicy: ";
for (int i = 0; i < n; ++i) {
cin >> num;
arr.push_back(num);
}
cout << "Tablica przed sortowaniem: ";
printArray(arr);
bubbleSort(arr); // Sortujemy tablicę
cout << "Tablica po sortowaniu: ";
printArray(arr);
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: