```
x = c( 3 3 , 55 , 22 , 44 , 11 )
for ( i in 1 : ( length ( x ) −1 ) ) {
for ( j in 1 : ( length ( x ) −1 ) ) {
if ( x [ j ] > x [ j + 1] ) {
tmp=x[ j ]
x [ j ] = x [ j +1]
x [ j +1] = tmp
}
}
print ( i )
print ( x )
}
```

The code starts by defining x as a numeric vector with five elements and then moves to two nested *for* loops that each iterate from 1 to 4 (because length(x) - 1 = 4). Let's see what happens on the first pass through the j loop when both i and j are 1. The *if* tests `x[1] > x[1+1]`

which is `x[1] > x[2]`

or `33 > 55`

. That is FALSE, so the code within the *if* is not executed and there is no other code in the j loop, so the loop runs the next iteration with j = 2. The *if* tests `x[2] > x[2+1]`

which is `x[2] > x[3]`

or `55 > 22`

. That is TRUE and the code within the *if* gets executed. The value of x[j], which is x[2] and equals 55, gets stored in *tmp*. Then the value of x[j+1], which is x[3] and equals 22, gets stored in x[2]. Finally, the value of *tmp* which is 55,gets stored in x[3]. So x[2] is now 22 and x[3] is 55. The x vector is now (33, 22, 55, 44, 11).

The j loop will now run again with j = 3, so x[j] = 55 and x[j+1] = 44. Because 55 > 44, the *if* condition will be TRUE. The two elements x[j] and x[j+1] will again be swapped and x will become (33, 22, 44, 55, 11).

The j loop will run one more time and you can work out what happens then.

Once the j loop has done its four iterations, the code prints the value of i and x, just so you can see how the algorithm progresses. Then i is changed to its next value and the whole j loop runs again.