Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
Line 156: | Line 156: | ||
==[[Python]]== |
==[[Python]]== |
||
[[Category:Python]] |
[[Category:Python]] |
||
<pre> |
|||
def bubblesort(seq): |
|||
for i in range(len(x) - 2): |
|||
for i in xrange(len(seq) - 2): |
|||
for j in range(i, len(seq) - 1): |
|||
if seq[j] > seq[j+1]: |
|||
seq[j], seq[j+1] = seq[j+1], seq[j] |
|||
return x |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Python has a built in sort method: |
|||
bubblesort(data) |
|||
⚫ | |||
</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/ |
|||
⚫ | |||
<pre> |
|||
⚫ | |||
>>> foo = [3, 5, 2, 6, 1] |
|||
>>> foo |
|||
[3, 5, 2, 6, 1] |
|||
>>> foo.sort() |
|||
⚫ | |||
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. |
|||
⚫ | |||
</pre> |
|||
==[[Ruby]]== |
==[[Ruby]]== |