Sorting algorithms/Bubble sort: Difference between revisions

→‎[[Python]]: removed off-topic example, changed formatting
(→‎[[Ruby]]: removed off-topic example)
(→‎[[Python]]: removed off-topic example, changed formatting)
Line 178:
==[[Python]]==
[[Category:Python]]
def bubblesort(seq):
<pre>
for i in xrange(len(seq) - 2):
def bubblesort(seq):
for ij in xrangerange(i, len(seq) - 21):
for j in range(i, len( if seq)[j] -> seq[j+1)]:
if seq[j], >seq[j+1] = seq[j+1]:, seq[j]
seq[j], seq[j+1] = seq[j+1], seq[j]
data = [3, 78, 4, 23, 6, 8, 6]
 
bubblesort(data)
data = [3, 78, 4, 23, 6, 8, 6]
print data # [3, 4, 6, 6, 8, 23, 78]
bubblesort(data)
print data # [3, 4, 6, 6, 8, 23, 78]
</pre>
Python has a built in sort method, it's a quite modified Merge Sort called <b>Timsort</b>: http://py-stablesort.sourceforge.net/
<pre>
>>> foo = [3, 5, 2, 6, 1]
>>> sorted(foo)
[1, 2, 3, 5, 6]
>>> foo
[3, 5, 2, 6, 1]
>>> foo.sort()
>>> foo
[1, 2, 3, 5, 6]
</pre>
 
==[[Ruby]]==
Anonymous user