Skip to content
This repository has been archived by the owner on Nov 6, 2018. It is now read-only.

Commit

Permalink
Added insertion sort
Browse files Browse the repository at this point in the history
  • Loading branch information
nryoung committed Aug 21, 2012
0 parents commit 49609cc
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.pyc
algo_venv/
Empty file added README.rst
Empty file.
Empty file added algorithms/__init__.py
Empty file.
Empty file added algorithms/sorting/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions algorithms/sorting/bubble_sort.py
@@ -0,0 +1,12 @@
""" Implementation of Bubble Sort. """

def sort(seq):

for x in seq:
temp = 0
for n in range(1, len(seq)):
temp = seq[n]
if seq[n] < seq[n-1]:
seq[n] = seq[n-1]
seq[n-1] = temp
return seq
11 changes: 11 additions & 0 deletions algorithms/sorting/insertion_sort.py
@@ -0,0 +1,11 @@
""" Implementation of Insertion Sort """

def sort(seq):
for n in range(1, len(seq)):
item = seq[n]
hole = n
while hole > 0 and seq[ hole - 1] > item:
seq[hole] = seq[hole - 1]
hole = hole - 1
seq[hole] = item
return seq
16 changes: 16 additions & 0 deletions algorithms/sorting/selection_sort.py
@@ -0,0 +1,16 @@
""" Implementation of Selection Sort. """

def sort(seq):

for i in range(0, len(seq)):
minat = i
minum = seq[i]
for j in range(i+1, len(seq)):
if minum > seq[j]:
minat = j
minum = seq[j]
temp = seq[i]
seq[i] = seq[minat]
seq[minat] = temp

return seq
Empty file added algorithms/tests/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions algorithms/tests/test_sorting.py
@@ -0,0 +1,36 @@
import random
import unittest
from ..sorting import bubble_sort, selection_sort, insertion_sort

class TestBubbleSort(unittest.TestCase):
"""
Tests Bubble sort on a small range from 0-9
"""
def test_bubblesort(self):
self.seq = range(10)
random.shuffle(self.seq)
rv = bubble_sort.sort(self.seq)
self.assertIs(self.seq[0], 0)
self.assertIs(self.seq[-1], 9)

class TestSelectionSort(unittest.TestCase):
"""
Tests Selection sort on a small range from 0-9
"""
def test_selectionsort(self):
self.seq = range(10)
random.shuffle(self.seq)
rv = selection_sort.sort(self.seq)
self.assertIs(self.seq[0], 0)
self.assertIs(self.seq[-1], 9)

class TestInsertionSort(unittest.TestCase):
"""
Tests Insertion sort on a small range from 0-9
"""
def test_selectionsort(self):
self.seq = range(10)
random.shuffle(self.seq)
rv = insertion_sort.sort(self.seq)
self.assertIs(self.seq[0], 0)
self.assertIs(self.seq[-1], 9)
4 changes: 4 additions & 0 deletions run_tests.py
@@ -0,0 +1,4 @@
import nose

if __name__ == '__main__':
nose.main()

0 comments on commit 49609cc

Please sign in to comment.