Sorting algorithms/Bubble sort: Difference between revisions

Line 156:
==[[Python]]==
[[Category:Python]]
<pre>
def bubblesort(xseq):
for i in range(len(x) - 2):
for ji in rangexrange(i, len(xseq) - 12):
iffor x[j] >in range(i, len(seq) - x[j+1]):
x[j], x if seq[j+1] => xseq[j+1], x[j]:
seq[j], seq[j+1] = seq[j+1], seq[j]
return x
x = [3, 78, 4, 23, 6, 8, 6]
bubblesort(x) # [3, 4, 6, 6, 8, 23, 78]
 
xdata = [3, 78, 4, 23, 6, 8, 6]
Python has a built in sort method:
bubblesort(data)
 
print bubblesort(x)data # [3, 4, 6, 6, 8, 23, 78]
 
</pre>
>>> foo = [3, 5, 2, 6, 1]
Python has a built in sort method, it's a quite modified Merge Sort called <b>Timsort</b>: http://py-stablesort.sourceforge.net/
>>> foo
<pre>
[3, 5, 2, 6, 1]
>>> foo.sort() = [3, 5, 2, 6, 1]
>>> foo
[13, 25, 32, 56, 61]
>>> foo.sort()
 
>>> foo
The built-in sort is not a bubble sort. TODO: I'm not sure what sort the python built-in is, but I'd imagine its fairly efficient.
[1, 2, [3, 5, 2, 6, 1]
</pre>
 
==[[Ruby]]==
Anonymous user