Skip to content

Instantly share code, notes, and snippets.

@fedeisas
Last active February 17, 2017 19:19
Show Gist options
  • Save fedeisas/a4226777188ada1b15f64eeeb37514db to your computer and use it in GitHub Desktop.
Save fedeisas/a4226777188ada1b15f64eeeb37514db to your computer and use it in GitHub Desktop.
Python Bubblesort
import unittest
def bubblesort(input):
swapped = True
while swapped:
swapped = False
for i in xrange(0, len(input) - 1):
if input[i] > input[i + 1]:
swapped = True
temp = input[i]
input[i] = input[i + 1]
input[i + 1] = temp
return input
class TestBubblesort(unittest.TestCase):
def test_empty(self):
self.assertEqual([], bubblesort([]))
def test_single(self):
self.assertEqual([1], bubblesort([1]))
def test_reverse(self):
self.assertEqual([1, 2, 3, 4, 5, 6, 7], bubblesort([7, 6, 5, 4, 3, 2, 1]))
def test_random(self):
self.assertEqual([1, 2, 3, 4, 5, 6, 7], bubblesort([5, 3, 2, 4, 7, 1, 6]))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment