Hide
Solve a problem – Filter by language, license, keyword, owner, or search text to find code & info fast. Join Siafoo Now or Learn More

Bubble Sort Atom Feed 0

In Brief A simple implementation of the Bubble Sort algorithm.... more
# 's
 1def bubble_sort(array):
2
3 swapped = True
4
5 # Keep making passes unless nothing is changed for an entire pass
6 while swapped == True:
7
8 swapped = False
9
10 # Step over array
11 for i in xrange(len(array) - 1):
12
13 if array[i] > array[i+1]:
14 array[i], array[i+1] = array[i+1], array[i] #That's right I just used a tuple to swap things
15 # Mark as changed
16 swapped = True
17
18 return array

A simple implementation of the Bubble Sort algorithm.

Pass it a list or anything indexable in Python and it will reorder it in place and pass it back.