Python program to Bubble Sort Elements of an Array

from random import randint
x = 20
y = []
for i in range(20):
 y.append(randint(1,20))
print(y)
for i in range(x):
    for j in range(x-i-1):
        if y[j] > y[j + 1]:
             z = y[j]
             y[j] = y[j+1]
             y[j+1] = z
print(y)

 

 

 

Output:

[3, 17, 19, 19, 10, 18, 4, 6, 5, 3, 6, 17, 11, 6, 5, 6, 20, 10, 18, 5]
[3, 3, 4, 5, 5, 5, 6, 6, 6, 6, 10, 10, 11, 17, 17, 18, 18, 19, 19, 20]

 

Subscribe For Daily Updates

100+ Python Pattern Examplespython pattern examples - star patterns, number patterns