Reference implementation of Bubble Sort. Select a language tab to view and copy the code.
void bubble_sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
#include <vector>
#include <algorithm>
void bubbleSort(std::vector<int>& arr) {
int n = static_cast<int>(arr.size());
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
}
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
def bubble_sort(arr: list[int]) -> None:
n = len(arr)
for i in range(n - 1):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
}
Python — Code Walkthrough
1
Type hints
arr: list[int]) -> None
PEP 484 annotations. The function modifies the list in-place and returns nothing (None). Lists in Python are mutable objects passed by reference, so the original list is sorted directly.
2
Getting the length
n = len(arr)
Python's built-in len() returns the number of elements. Caching it in n avoids calling len(arr) on every loop iteration.
3
range() instead of a C-style counter
for i in range(n - 1)
range(n − 1) generates integers 0 through n−2 inclusive. No explicit initialisation, condition, or increment needed — Python handles it idiomatically.
4
Shrinking inner range
for j in range(n - i - 1)
Each outer iteration reduces the inner range by one, skipping the already-sorted elements at the tail end of the list.
5
Tuple swap — no temp variable
arr[j], arr[j + 1] = arr[j + 1], arr[j]
Python's simultaneous assignment evaluates both right-hand side values before any assignment occurs. This means no temporary variable is needed — the cleanest swap of all five languages.
Function signature void bubble_sort(int arr[], int n) Takes a raw array pointer and its length n. Returns void — the sort is done in-place, directly modifying the original array.Loop variables int i, j, temp i counts passes over the array, j is the inner scan cursor that compares adjacent pairs, and temp is a scratch variable used during each swap.Outer loop — pass counter for (i = 0; i < n - 1; i++) Runs n−1 passes. After each full pass, the largest unsorted element has bubbled to its final position at the end, so one fewer comparison is needed each time.Inner loop — shrinking window for (j = 0; j < n - i - 1; j++) Scans from index 0 up to the last unsorted position. The − i term shrinks the window each pass because the last i elements are already in their correct sorted positions.Swap with a temporary variable temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; C has no built-in swap, so a classic three-step exchange is used. temp holds arr[j] while arr[j] is overwritten, preventing data loss.
Vector reference parameter std::vector<int>& arr Passed by reference (&) so changes affect the original vector. Using std::vector instead of a raw array provides bounds-safe access and automatic memory management.Casting size to int int n = static_cast<int>(arr.size()) arr.size() returns size_t which is an unsigned type. Explicitly casting to int prevents signed/unsigned comparison warnings when n is used in loop conditions.Same outer and inner loop structure for (int i = 0; i < n - 1; i++) The nested loop logic is identical to the C version — n−1 outer passes, each with a shrinking inner window of size n − i − 1.std::swap replaces the temp variable std::swap(arr[j], arr[j + 1]) The C++ standard library swap handles the exchange in a single expressive call. It is equivalent to the three-line temp pattern in C but cleaner and less error-prone.
Static utility method public static void bubbleSort(int[] arr) Declared static so it can be called without instantiating the class: BubbleSort.bubbleSort(myArray). Accepts a primitive int[] — Java passes arrays by reference, so the original is sorted in-place.Array length as a field int n = arr.length Java arrays carry their length as a built-in field. Unlike C there is no need to pass the size as a separate parameter — arr.length is always available.Identical nested loop bounds for (int j = 0; j < n - i - 1; j++) The algorithm logic is the same across all languages. The inner bound n − i − 1 ensures already-sorted tail elements are never re-examined.Manual swap with temp int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; Java does not have a built-in swap for primitive arrays (Collections.swap only works on object lists), so the classic three-variable pattern is used here.
Caching the length const n = arr.length Storing the length once in a const avoids reading arr.length on every loop iteration and makes the intent clear. The array is sorted in-place — no copy is returned.Block-scoped counters with let for (let i = 0; i < n - 1; i++) Using let (ES6+) instead of var keeps i and j scoped to the for block, preventing accidental leaks into the surrounding function scope.Shrinking inner loop for (let j = 0; j < n - i - 1; j++) The inner bound shrinks by 1 each outer pass, skipping the i elements that have already bubbled to their final positions at the end of the array.Destructuring swap [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]] ES6 array destructuring assignment reads both values on the right first, then assigns them simultaneously — exactly like Python tuple swap. No temp variable needed, and it reads naturally as "swap these two positions".