Sorting
Bubble Sort
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.ts
Replay: real traced execution (multi-file project)
const arr: number[] = [5, 1, 4, 2, 8];
const n: number = arr.length;
for (let i: number = 0; i < n - 1; i++) {
let swapped: boolean = false;
for (let j: number = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
const tmp: number = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
console.log(JSON.stringify(arr));
arr ← [5, 1, 4, 2, 8]
1const arr: number[] = [5, 1, 4, 2, 8];2const n: number = arr.length;values this step[5, 1, 4, 2, 8]arrn ← 5
1const arr: number[] = [5, 1, 4, 2, 8];2const n: number = arr.length;3for (let i: number = 0; i < n - 1; i++) {values this step5n[5, 1, 4, 2, 8]arrswapped ← false
3for (let i: number = 0; i < n - 1; i++) {4 let swapped: boolean = false;5 for (let j: number = 0; j < n - i - 1; j++) {values this stepfalseswappedarr[j] > arr[j+1] ← true
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this steptruearr[j] > arr[j+1][5, 1, 4, 2, 8]arr0j5arr[j]1arr[j+1]arr ← [1, 5, 4, 2, 8], swapped ← true
8arr[j] = arr[j + 1];9arr[j + 1] = tmp;10swapped = true;values this step[5, 1, 4, 2, 8] → [1, 5, 4, 2, 8]arrtrueswappedarr[j] > arr[j+1] ← true
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this steptruearr[j] > arr[j+1][1, 5, 4, 2, 8]arr1j5arr[j]4arr[j+1]arr ← [1, 4, 5, 2, 8], swapped ← true
8arr[j] = arr[j + 1];9arr[j + 1] = tmp;10swapped = true;values this step[1, 5, 4, 2, 8] → [1, 4, 5, 2, 8]arrtrueswappedarr[j] > arr[j+1] ← true
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this steptruearr[j] > arr[j+1][1, 4, 5, 2, 8]arr2j5arr[j]2arr[j+1]arr ← [1, 4, 2, 5, 8], swapped ← true
8arr[j] = arr[j + 1];9arr[j + 1] = tmp;10swapped = true;values this step[1, 4, 5, 2, 8] → [1, 4, 2, 5, 8]arrtrueswappedarr[j] > arr[j+1] ← false
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this stepfalsearr[j] > arr[j+1][1, 4, 2, 5, 8]arr3j5arr[j]8arr[j+1]swapped ← false
3for (let i: number = 0; i < n - 1; i++) {4 let swapped: boolean = false;5 for (let j: number = 0; j < n - i - 1; j++) {values this stepfalseswappedarr[j] > arr[j+1] ← false
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this stepfalsearr[j] > arr[j+1][1, 4, 2, 5, 8]arr0j1arr[j]4arr[j+1]arr[j] > arr[j+1] ← true
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this steptruearr[j] > arr[j+1][1, 4, 2, 5, 8]arr1j4arr[j]2arr[j+1]arr ← [1, 2, 4, 5, 8], swapped ← true
8arr[j] = arr[j + 1];9arr[j + 1] = tmp;10swapped = true;values this step[1, 4, 2, 5, 8] → [1, 2, 4, 5, 8]arrtrueswappedarr[j] > arr[j+1] ← false
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr2j4arr[j]5arr[j+1]swapped ← false
3for (let i: number = 0; i < n - 1; i++) {4 let swapped: boolean = false;5 for (let j: number = 0; j < n - i - 1; j++) {values this stepfalseswappedarr[j] > arr[j+1] ← false
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr0j1arr[j]2arr[j+1]arr[j] > arr[j+1] ← false
5for (let j: number = 0; j < n - i - 1; j++) {6 if (arr[j] > arr[j + 1]) {7 const tmp: number = arr[j];values this stepfalsearr[j] > arr[j+1][1, 2, 4, 5, 8]arr1j2arr[j]4arr[j+1]loop ← break
12}13if (!swapped) {14 break;values this stepbreakloopfalseswappedstdout ← [1, 2, 4, 5, 8]
16}17console.log(JSON.stringify(arr));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
- TypeScript: nested
forloops with the early-exitswapped: booleanflag. Never callarr.sort(); the lesson is teaching the comparison-and-swap. - Annotate the boolean explicitly so the early-exit invariant is clear.
- The replay distinguishes compare frames from swap frames so the moving
pivot value is visible. The pass number and
swappedflag appear in the trace.