Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
(+python)
(Properly ordering the languages)
Line 150: Line 150:


Note: Of course, there's no need to implement bubble sort in Perl as it has sort built-in.
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]]==
==[[Ruby]]==
Line 178: Line 190:
puts ary.sort!
puts ary.sort!
# => [3, 4, 6, 6, 8, 23, 78]
# => [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]