Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
(→[[Ruby]]: removed off-topic example) |
(→[[Python]]: removed off-topic example, changed formatting) |
||
Line 178: | Line 178: | ||
==[[Python]]== |
==[[Python]]== |
||
[[Category:Python]] |
[[Category:Python]] |
||
⚫ | |||
<pre> |
|||
for i in xrange(len(seq) - 2): |
|||
⚫ | |||
for |
for j in range(i, len(seq) - 1): |
||
if seq[j] > seq[j+1]: |
|||
seq[j], seq[j+1] = seq[j+1], seq[j] |
|||
seq[j], seq[j+1] = seq[j+1], seq[j] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</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]]== |
==[[Ruby]]== |