Bubble Sort
Loading editor...
#include <bits/stdc++.h>
using namespace std;
// Bubble sort that works on a copy of the passed vector
void bubbleSort(vector<int> &array) {
int n = array.size(); // Number of elements in the vector
// Outer loop: after each pass, the largest unsorted element bubbles to its final position
for (int i = 0; i < n-1; i++) {
// Inner loop: compare adjacent pairs up to the last unsorted element
for (int j = 0; j < n-i-1; j++) {
// If the current element is greater than the next, swap them
if (array[j] > array[j+1]) {
swap(array[j], array[j+1]); // Standard library swap exchanges the two values
}
}
}
List of numbers to sort, space-separated.