bubbleSort(A, n)
begin
for i ← 0 to n-2 do
swapped ← false
for j ← 0 to n-i-2 do
if A[j] > A[j+1] then
swap A[j] ↔ A[j+1]
swapped ← true
if not swapped then
return // early exit
end