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.rs
Replay: real traced execution (multi-file project)
fn main() {
	let mut arr = [5, 1, 4, 2, 8];
	let n = arr.len();
	for i in 0..n-1 {
		let mut swapped = false;
		for j in 0..n-i-1 {
			if arr[j] > arr[j+1] {
				let tmp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = tmp;
				swapped = true;
			}
		}
		if !swapped {
			break;
		}
	}
	println!("{:?}", arr);
}
  1. arr ← [5, 1, 4, 2, 8]

    1fn main() {2	let mut arr = [5, 1, 4, 2, 8];3	let n = arr.len();
    values this step[5, 1, 4, 2, 8]arr
  2. n ← 5

    2let mut arr = [5, 1, 4, 2, 8];3let n = arr.len();4for i in 0..n-1 {
    values this step5n[5, 1, 4, 2, 8]arr
  3. swapped ← false

    4for i in 0..n-1 {5	let mut swapped = false;6	for j in 0..n-i-1 {
    values this stepfalseswapped
  4. arr[j] > arr[j+1] ← true

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let 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

    8let tmp = arr[j];9arr[j] = arr[j+1];10arr[j+1] = tmp;
    values this step[5, 1, 4, 2, 8] [1, 5, 4, 2, 8]arrtrueswapped
  6. arr[j] > arr[j+1] ← true

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let 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

    8let tmp = arr[j];9arr[j] = arr[j+1];10arr[j+1] = tmp;
    values this step[1, 5, 4, 2, 8] [1, 4, 5, 2, 8]arrtrueswapped
  8. arr[j] > arr[j+1] ← true

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let 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

    8let tmp = arr[j];9arr[j] = arr[j+1];10arr[j+1] = tmp;
    values this step[1, 4, 5, 2, 8] [1, 4, 2, 5, 8]arrtrueswapped
  10. arr[j] > arr[j+1] ← false

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 4, 2, 5, 8]arr3j5arr[j]8arr[j+1]
  11. swapped ← false

    4for i in 0..n-1 {5	let mut swapped = false;6	for j in 0..n-i-1 {
    values this stepfalseswapped
  12. arr[j] > arr[j+1] ← false

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let 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

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let 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

    8let tmp = arr[j];9arr[j] = arr[j+1];10arr[j+1] = tmp;
    values this step[1, 4, 2, 5, 8] [1, 2, 4, 5, 8]arrtrueswapped
  15. arr[j] > arr[j+1] ← false

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr2j4arr[j]5arr[j+1]
  16. swapped ← false

    4for i in 0..n-1 {5	let mut swapped = false;6	for j in 0..n-i-1 {
    values this stepfalseswapped
  17. arr[j] > arr[j+1] ← false

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let 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

    6for j in 0..n-i-1 {7	if arr[j] > arr[j+1] {8		let tmp = arr[j];
    values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr1j2arr[j]4arr[j+1]
  19. loop ← break

    14if !swapped {15	break;16}
    values this stepbreakloopfalseswapped
  20. stdout ← [1, 2, 4, 5, 8]

    17	}18	println!("{:?}", arr);19}
    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

  • Rust: nested for loops with the early-exit swapped flag. The standard arr.sort() would hide the comparison-and-swap the lesson is teaching.
  • The explicit let tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; three-line swap keeps the move visible without leaning on arr.swap(j, j+1).
  • The replay distinguishes compare frames from swap frames so the moving pivot value is visible. The pass number and swapped flag appear in the trace.