Sorting
Bubble Sort
Repeatedly walk the vector 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 c(5, 1, 4, 2, 8) finishes after three passes: two with
swaps, then a clean pass that triggers the early exit. Final vector
c(1, 2, 4, 5, 8).
adjacent-pair compare and swap
Inner loop walks `j` from `1` to `n - i` comparing `arr[j]` and `arr[j + 1]`.
early exit
A `swapped` flag set `FALSE` at the start of each pass. If no swap happened, flip a `done` flag and break out of the outer loop.
Basic Implementation
basic.R
Replay: real traced execution (multi-file project)
arr <- c(5, 1, 4, 2, 8)
n <- length(arr)
i <- 1
done <- FALSE
while (i <= n - 1 && !done) {
swapped <- FALSE
j <- 1
while (j <= n - i) {
if (arr[j] > arr[j + 1]) {
tmp <- arr[j]
arr[j] <- arr[j + 1]
arr[j + 1] <- tmp
swapped <- TRUE
}
j <- j + 1
}
if (!swapped) {
done <- TRUE
}
i <- i + 1
}
cat("[", paste(arr, collapse = ", "), "]\n", sep = "")
arr ← [5, 1, 4, 2, 8]
1arr <- c(5, 1, 4, 2, 8)2n <- length(arr)values this step[5, 1, 4, 2, 8]arrn ← 5
1arr <- c(5, 1, 4, 2, 8)2n <- length(arr)3i <- 1values this step5n[5, 1, 4, 2, 8]arrswapped ← false
5while (i <= n - 1 && !done) {6 swapped <- FALSE7 j <- 1values this stepfalseswappedarr[j] > arr[j+1] ← true
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this steptruearr[j] > arr[j+1][5, 1, 4, 2, 8]arr1j5arr[j]1arr[j+1]arr ← [1, 5, 4, 2, 8], swapped ← true
10tmp <- arr[j]11arr[j] <- arr[j + 1]12arr[j + 1] <- tmpvalues this step[5, 1, 4, 2, 8] → [1, 5, 4, 2, 8]arrtrueswappedarr[j] > arr[j+1] ← true
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this steptruearr[j] > arr[j+1][1, 5, 4, 2, 8]arr2j5arr[j]4arr[j+1]arr ← [1, 4, 5, 2, 8], swapped ← true
10tmp <- arr[j]11arr[j] <- arr[j + 1]12arr[j + 1] <- tmpvalues this step[1, 5, 4, 2, 8] → [1, 4, 5, 2, 8]arrtrueswappedarr[j] > arr[j+1] ← true
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this steptruearr[j] > arr[j+1][1, 4, 5, 2, 8]arr3j5arr[j]2arr[j+1]arr ← [1, 4, 2, 5, 8], swapped ← true
10tmp <- arr[j]11arr[j] <- arr[j + 1]12arr[j + 1] <- tmpvalues this step[1, 4, 5, 2, 8] → [1, 4, 2, 5, 8]arrtrueswappedarr[j] > arr[j+1] ← false
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this stepfalsearr[j] > arr[j+1][1, 4, 2, 5, 8]arr4j5arr[j]8arr[j+1]swapped ← false
5while (i <= n - 1 && !done) {6 swapped <- FALSE7 j <- 1values this stepfalseswappedarr[j] > arr[j+1] ← false
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this stepfalsearr[j] > arr[j+1][1, 4, 2, 5, 8]arr1j1arr[j]4arr[j+1]arr[j] > arr[j+1] ← true
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this steptruearr[j] > arr[j+1][1, 4, 2, 5, 8]arr2j4arr[j]2arr[j+1]arr ← [1, 2, 4, 5, 8], swapped ← true
10tmp <- arr[j]11arr[j] <- arr[j + 1]12arr[j + 1] <- tmpvalues this step[1, 4, 2, 5, 8] → [1, 2, 4, 5, 8]arrtrueswappedarr[j] > arr[j+1] ← false
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr3j4arr[j]5arr[j+1]swapped ← false
5while (i <= n - 1 && !done) {6 swapped <- FALSE7 j <- 1values this stepfalseswappedarr[j] > arr[j+1] ← false
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr1j1arr[j]2arr[j+1]arr[j] > arr[j+1] ← false
8while (j <= n - i) {9 if (arr[j] > arr[j + 1]) {10 tmp <- arr[j]values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr2j2arr[j]4arr[j+1]done ← true
17if (!swapped) {18 done <- TRUE19}values this steptruedonefalseswappedstdout ← [1, 2, 4, 5, 8]
21}22cat("[", paste(arr, collapse = ", "), "]\n", sep = "")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
- R: explicit
whileloops withi,j,done, andswappedso the early-exit flow stays visible. The stdlibsort(arr)is implemented in C and would hide the comparison-and-swap the lesson is teaching, andorder(arr)only returns indices — neither shows the moving pivot value the lesson highlights. - The explicit
tmp <- arr[j]; arr[j] <- arr[j+1]; arr[j+1] <- tmpthree-line swap keeps the move visible without leaning on R's vectorised swaparr[c(j, j+1)] <- arr[c(j+1, j)]. - The replay distinguishes compare frames from swap frames so the
moving pivot value is visible. The pass number and
swappedflag appear in the trace.