You are on page 1of 34

The Bubble-Sort Algorithm

BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

1 2 3 4 5

9 3 12 1 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

1 2 3 4 5

9 3 12 1 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 3 12 1 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] False
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 3 12 1 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 3 12 1 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] True
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 3 12 1 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 3 1 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 3 1 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] True
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 3 1 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 1 3 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 1 3 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] True
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

9 1 3 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

ij

1 2 3 4 5

1 9 3 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] True
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 12 6
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] False
then excenge A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] True
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 9 3 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 9 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

ij

1 2 3 4 5

1 3 9 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 9 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] False
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 9 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 9 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] True
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 9 6 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 6 9 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

ij

1 2 3 4 5

1 3 6 9 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 6 9 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1] False
then exchange A[j] <-> A[j-1]

i j

1 2 3 4 5

1 3 6 9 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

ij

1 2 3 4 5

1 3 6 9 12
BUBBLESORT(A)
for i <- 1 to length[A]
do for j <- length[A] downto i + 1
do if A[j] < A[j-1]
then exchange A[j] <-> A[j-1]

The END

ij

1 2 3 4 5

1 3 6 9 12

You might also like