Bubble Sort |
A sorting algorithm that works by repeatedly swapping adjacent elements in a list until they are in the correct order. It gets its name from the way smaller elements "bubble" to the top of the list during each pass.
Time Complexity: O(n²)
void bubble_sort(vector<int> &array) { for (int i = 0; i < array.size() - 1; i++) { for (int j = 0; j < array.size() - 1; j++) { if (array.at(j) > array.at(j + 1)) { int temp = array.at(j); array.at(j) = array.at(j + 1); array.at(j + 1) = temp; } } } }