numbers = [9,1,2,3,45,4332,3232,29,-1]
for j in range(len(numbers)):
for i in range(len(numbers)-1):
if(numbers[i] > numbers[i+1]):
numbers[i],numbers[i+1] = numbers[i+1],numbers[i]
First setup the array with numbers not in any order
numbers = [9,1,2,3,45,4332,3232,29,-1]
Now create a for loop that loops depending on the amount numbers - 1 as the array index starts at 0.
for j in range(len(numbers)):
Another for loop nested inside the for loop to do the same thing.
for i in range(len(numbers)-1):
Compare the value of the current number with the number that is next in array.
if(numbers[i] > numbers[i+1]):
If this is true then swap them around so that the smaller one is first.
numbers[i],numbers[i+1] = numbers[i+1],numbers[i]
The array before the iteration starts looks like this
[9, 1, 2, 3, 45, 4332, 3232, 29, -1]
After the code runs it is in order like this
[-1, 1, 2, 3, 9, 29, 45, 3232, 4332]