for i = 0 to n-1 j = n-1 sorted = true while (j > i) if (x[j-1] > x[j]) swap(x, j-1, j) sorted = false j-- if sorted break