Sorting algorithms/Bubble sort: Difference between revisions
Properly ordering the languages
(+python) |
(Properly ordering the languages) |
||
Line 150:
Note: Of course, there's no need to implement bubble sort in Perl as it has sort built-in.
==[[Python]]==▼
def bubblesort(x):▼
for i in range(len(x) - 2):▼
for j in range(i, len(x) - 1):▼
if x[j] > x[j+1]:▼
x[j], x[j+1] = x[j+1], x[j]▼
return x▼
x = [3, 78, 4, 23, 6, 8, 6]▼
bubblesort(x) # [3, 4, 6, 6, 8, 23, 78]▼
==[[Ruby]]==
Line 178 ⟶ 190:
puts ary.sort!
# => [3, 4, 6, 6, 8, 23, 78]
▲==[[Python]]==
▲ def bubblesort(x):
▲ for i in range(len(x) - 2):
▲ for j in range(i, len(x) - 1):
▲ if x[j] > x[j+1]:
▲ x[j], x[j+1] = x[j+1], x[j]
▲ return x
▲ x = [3, 78, 4, 23, 6, 8, 6]
▲ bubblesort(x) # [3, 4, 6, 6, 8, 23, 78]
|