{article Examples__Python 3.4 }{title} {text}{/article}
def bubblesort(lst):
"Sorts lst in place and returns it."
for passesLeft in range(len(lst)-1, 0, -1):
for index in range(passesLeft):
if lst[index] > lst[index + 1]:
lst[index], lst[index + 1] = lst[index + 1], lst[index]
return lst
lst = [54,26,100,17,77,1,44,55,20]
bubblesort(lst)
[1, 17, 20, 26, 44, 54, 55, 77, 100]
print(lst)
[1, 17, 20, 26, 44, 54, 55, 77, 100]
>>>
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
def bubblesort(lst):
"Sorts lst in place and returns it."
for passesLeft in range(len(lst)-1, 0, -1):
for index in range(passesLeft):
if lst[index] > lst[index + 1]:
lst[index], lst[index + 1] = lst[index + 1], lst[index]
return lst
lst = [54,26,100,17,77,1,44,55,20]
bubblesort(lst)
[1, 17, 20, 26, 44, 54, 55, 77, 100]
print(lst)
[1, 17, 20, 26, 44, 54, 55, 77, 100]
>>>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}