Repeatedly walk the array comparing adjacent pairs and swapping any that are out of order. After pass k, the k largest elements are in their final positions at the end. Stop early when a full pass makes zero swaps.

Algorithm

Canonical input [5, 1, 4, 2, 8] finishes after three passes: two with swaps, then a clean pass that triggers the early exit. Final array [1, 2, 4, 5, 8].

adjacent-pair compare and swap Inner loop walks `j` from `0` to `n - i - 2` comparing `arr[j]` and `arr[j + 1]`.
early exit A `swapped` flag set false at the start of each pass. If no swap happened, break out of the outer loop.

Basic Implementation

basic.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <vector>

int main() {
    std::vector<int> arr = {5, 1, 4, 2, 8};
    int n = static_cast<int>(arr.size());
    for (int i = 0; i < n - 1; ++i) {
        bool swapped = false;
        for (int j = 0; j < n - i - 1; ++j) {
            if (arr[j] > arr[j + 1]) {
                int tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
                swapped = true;
            }
        }
        if (!swapped) {
            break;
        }
    }
    std::cout << "[";
    for (size_t i = 0; i < arr.size(); ++i) {
        if (i > 0) std::cout << ", ";
        std::cout << arr[i];
    }
    std::cout << "]" << std::endl;
    return 0;
}
  1. arr ← [5, 1, 4, 2, 8]

    4int main() {5    std::vector<int> arr = {5, 1, 4, 2, 8};6    int n = static_cast<int>(arr.size());
    values this step[5, 1, 4, 2, 8]arr
  2. n ← 5

    5std::vector<int> arr = {5, 1, 4, 2, 8};6int n = static_cast<int>(arr.size());7for (int i = 0; i < n - 1; ++i) {
    values this step5n[5, 1, 4, 2, 8]arr
  3. swapped ← false

    7for (int i = 0; i < n - 1; ++i) {8    bool swapped = false;9    for (int j = 0; j < n - i - 1; ++j) {
    values this stepfalseswapped
  4. arr[j] > arr[j+1] ← true

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this steptruearr[j] > arr[j+1][5, 1, 4, 2, 8]arr0j5arr[j]1arr[j+1]
  5. arr ← [1, 5, 4, 2, 8], swapped ← true

    12arr[j] = arr[j + 1];13arr[j + 1] = tmp;14swapped = true;
    values this step[5, 1, 4, 2, 8] [1, 5, 4, 2, 8]arrtrueswapped
  6. arr[j] > arr[j+1] ← true

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this steptruearr[j] > arr[j+1][1, 5, 4, 2, 8]arr1j5arr[j]4arr[j+1]
  7. arr ← [1, 4, 5, 2, 8], swapped ← true

    12arr[j] = arr[j + 1];13arr[j + 1] = tmp;14swapped = true;
    values this step[1, 5, 4, 2, 8] [1, 4, 5, 2, 8]arrtrueswapped
  8. arr[j] > arr[j+1] ← true

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this steptruearr[j] > arr[j+1][1, 4, 5, 2, 8]arr2j5arr[j]2arr[j+1]
  9. arr ← [1, 4, 2, 5, 8], swapped ← true

    12arr[j] = arr[j + 1];13arr[j + 1] = tmp;14swapped = true;
    values this step[1, 4, 5, 2, 8] [1, 4, 2, 5, 8]arrtrueswapped
  10. arr[j] > arr[j+1] ← false

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 4, 2, 5, 8]arr3j5arr[j]8arr[j+1]
  11. swapped ← false

    7for (int i = 0; i < n - 1; ++i) {8    bool swapped = false;9    for (int j = 0; j < n - i - 1; ++j) {
    values this stepfalseswapped
  12. arr[j] > arr[j+1] ← false

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 4, 2, 5, 8]arr0j1arr[j]4arr[j+1]
  13. arr[j] > arr[j+1] ← true

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this steptruearr[j] > arr[j+1][1, 4, 2, 5, 8]arr1j4arr[j]2arr[j+1]
  14. arr ← [1, 2, 4, 5, 8], swapped ← true

    12arr[j] = arr[j + 1];13arr[j + 1] = tmp;14swapped = true;
    values this step[1, 4, 2, 5, 8] [1, 2, 4, 5, 8]arrtrueswapped
  15. arr[j] > arr[j+1] ← false

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr2j4arr[j]5arr[j+1]
  16. swapped ← false

    7for (int i = 0; i < n - 1; ++i) {8    bool swapped = false;9    for (int j = 0; j < n - i - 1; ++j) {
    values this stepfalseswapped
  17. arr[j] > arr[j+1] ← false

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr0j1arr[j]2arr[j+1]
  18. arr[j] > arr[j+1] ← false

    9for (int j = 0; j < n - i - 1; ++j) {10    if (arr[j] > arr[j + 1]) {11        int tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr1j2arr[j]4arr[j+1]
  19. loop ← break

    17if (!swapped) {18    break;19}
    values this stepbreakloopfalseswapped
  20. stdout ← [1, 2, 4, 5, 8]

    25}26std::cout << "]" << std::endl;27return 0;
    values this step[1, 2, 4, 5, 8]stdout[1, 2, 4, 5, 8]arr

Complexity

  • Time: O(n^2) worst and average; O(n) best (already sorted with early exit)
  • Space: O(1)
  • Stable: yes

Implementation notes

  • In C++, the input is a std::vector<int> initialized as {5, 1, 4, 2, 8} and n is copied from static_cast<int>(arr.size()) for the loop bounds.
  • The outer loop uses int i and stops at i < n - 1; the inner loop uses int j and stops at j < n - i - 1, so each arr[j + 1] read is inside the vector.
  • Swaps are manual: int tmp = arr[j], then two assignments into adjacent vector slots. The checked source does not call std::swap.
  • bool swapped is reset each pass and set only after a vector mutation. The trace shows pass 1 moving through [1, 4, 2, 5, 8], pass 2 reaching [1, 2, 4, 5, 8], and pass 3 breaking with swapped == false.
  • Output is streamed with std::cout, a size_t print loop, comma separators, and std::endl, producing [1, 2, 4, 5, 8].
  • Visible allocation is the vector storage from the initializer list; mutation is limited to vector element assignments, the scalar tmp, and swapped.