diff --git a/PracticingAlgorithms/.gitignore b/PracticingAlgorithms/.gitignore new file mode 100644 index 0000000..cc694be --- /dev/null +++ b/PracticingAlgorithms/.gitignore @@ -0,0 +1,2 @@ +bin/* +*.o diff --git a/PracticingAlgorithms/Makefile b/PracticingAlgorithms/Makefile new file mode 100644 index 0000000..b8d6031 --- /dev/null +++ b/PracticingAlgorithms/Makefile @@ -0,0 +1,14 @@ +BINDIR := $(PWD)/bin +CFLAGS := -std=c99 -Wall -g + +export BINDIR CFLAGS + + +all: + cd src && $(MAKE) + +clean: + rm -f bin/* + + +.PHONY: all clean diff --git a/PracticingAlgorithms/README.md b/PracticingAlgorithms/README.md new file mode 100644 index 0000000..e37292d --- /dev/null +++ b/PracticingAlgorithms/README.md @@ -0,0 +1,10 @@ +# Practicing Algorithms + +I've been burned enough times when in a room with a bunch of CS Majors... +(grumble.) + +## MIT Open Courseware stuff + +It's just like bookmarks, but on github! + + - Intro. to Algorithms lecture 01 diff --git a/PracticingAlgorithms/bench b/PracticingAlgorithms/bench new file mode 100755 index 0000000..08808c7 --- /dev/null +++ b/PracticingAlgorithms/bench @@ -0,0 +1,10 @@ +#!/bin/bash + +SETUP="import sorting" +LIST="$(python -c "import random;l = list(range(0, ${1-5000}));random.shuffle(l);print(l)")" + +echo -n "insertion sort: " +python -m timeit -s "$SETUP" "sorting.insertion_sort($LIST)" + +echo -n "merge sort: " +python -m timeit -s "$SETUP" "sorting.merge_sort($LIST)" diff --git a/PracticingAlgorithms/bin/.keep b/PracticingAlgorithms/bin/.keep new file mode 100644 index 0000000..e69de29 diff --git a/PracticingAlgorithms/sorting.py b/PracticingAlgorithms/sorting.py new file mode 100644 index 0000000..cbddb26 --- /dev/null +++ b/PracticingAlgorithms/sorting.py @@ -0,0 +1,49 @@ +# vim:fileencoding=utf-8 +import math + + +def insertion_sort(a): + """ Θ(n^2) """ + for j in range(1, len(a)): + key = a[j] + i = j - 1 + while i >= 0 and a[i] > key: + a[i + 1] = a[i] + i = i - 1 + a[i + 1] = key + + +def merge_sort(m): + n = len(m) + if n <= 1: + return m + + middle = math.ceil(n / 2) + + left = m[:middle] + right = m[middle:] + + left = merge_sort(left) + right = merge_sort(right) + + return merge(left, right) + + +def merge(left, right): + result = [] + + while len(left) > 0 or len(right) > 0: + if len(left) > 0 and len(right) > 0: + if left[0] <= right[0]: + result.append(left.pop(0)) + else: + result.append(right.pop(0)) + + elif len(left) > 0: + result.append(left.pop(0)) + + elif len(right) > 0: + result.append(right.pop(0)) + + return result + diff --git a/PracticingAlgorithms/sorting.rb b/PracticingAlgorithms/sorting.rb new file mode 100644 index 0000000..f4f68fd --- /dev/null +++ b/PracticingAlgorithms/sorting.rb @@ -0,0 +1,12 @@ +def insertion_sort(a) + (0..(a.length - 1)).each do |j| + key = a[j] + i = j - 1 + while (i >= 0) && (a[i] > key) + a[i + 1] = a[i] + i = i - 1 + end + a[i + 1] = key + end +end + diff --git a/PracticingAlgorithms/src/Makefile b/PracticingAlgorithms/src/Makefile new file mode 100644 index 0000000..ae4a6d3 --- /dev/null +++ b/PracticingAlgorithms/src/Makefile @@ -0,0 +1,8 @@ +all: $(BINDIR)/insertion-sort + + +$(BINDIR)/insertion-sort: insertion-sort.c + gcc -o $@ $(CFLAGS) $^ + + +.PHONY: all diff --git a/PracticingAlgorithms/src/insertion-sort.c b/PracticingAlgorithms/src/insertion-sort.c new file mode 100644 index 0000000..be8594a --- /dev/null +++ b/PracticingAlgorithms/src/insertion-sort.c @@ -0,0 +1,38 @@ +#include + +void insertion_sort(int * a, int len_a); + + +int +main(int argc, char ** argv) +{ + int i; + int l[] = {1, 4, 8, 9, 2, 3}; + + insertion_sort(l, 6); + + for (i = 0; i < 6; i++) { + printf("%d\n", l[i]); + } +} + + +void +insertion_sort(int * a, int len_a) +{ + int i, j, key; + + for (j = 1; j < len_a; j++) { + key = a[j]; + i = j - 1; + while ((i >= 0) && (a[i] > key)) { + a[i + 1] = a[i]; + i = i - 1; + } + a[i + 1] = key; + } +} + + +/* vim:filetype=c + */ diff --git a/PracticingAlgorithms/test_sorting.py b/PracticingAlgorithms/test_sorting.py new file mode 100644 index 0000000..55bcbf4 --- /dev/null +++ b/PracticingAlgorithms/test_sorting.py @@ -0,0 +1,90 @@ +import random +import unittest + +import sorting + + +class TestInsertionSort(unittest.TestCase): + + def test_example(self): + tmpl = [8, 2, 4, 9, 3, 6] + a = tmpl[:] + expected = [2, 3, 4, 6, 8, 9] + + sorting.insertion_sort(a) + + self.assertEqual(expected, a) + + def test_100_example(self): + tmpl = list(range(0, 1000)) + a = tmpl[:] + random.shuffle(a) + expected = tmpl[:] + + sorting.insertion_sort(a) + + self.assertEqual(expected, a) + + def test_reversed_100_example(self): + tmpl = list(range(0, 1000)) + a = list(reversed(tmpl[:])) + expected = tmpl[:] + + sorting.insertion_sort(a) + + self.assertEqual(expected, a) + + def test_1000_example(self): + tmpl = list(range(0, 1000)) + a = tmpl[:] + random.shuffle(a) + expected = tmpl[:] + + sorting.insertion_sort(a) + + self.assertEqual(expected, a) + + +class TestMergeSort(unittest.TestCase): + + def test_example(self): + tmpl = [8, 2, 4, 9, 3, 6] + a = tmpl[:] + expected = [2, 3, 4, 6, 8, 9] + + actual = sorting.merge_sort(a) + + self.assertEqual(expected, actual) + + def test_100_example(self): + tmpl = list(range(0, 1000)) + a = tmpl[:] + random.shuffle(a) + expected = tmpl[:] + + actual = sorting.merge_sort(a) + + self.assertEqual(expected, actual) + + def test_reversed_100_example(self): + tmpl = list(range(0, 1000)) + a = list(reversed(tmpl[:])) + expected = tmpl[:] + + actual = sorting.merge_sort(a) + + self.assertEqual(expected, actual) + + def test_1000_example(self): + tmpl = list(range(0, 1000)) + a = tmpl[:] + random.shuffle(a) + expected = tmpl[:] + + actual = sorting.merge_sort(a) + + self.assertEqual(expected, actual) + + +if __name__ == '__main__': + unittest.main() diff --git a/PracticingAlgorithms/test_sorting.rb b/PracticingAlgorithms/test_sorting.rb new file mode 100644 index 0000000..4253cf3 --- /dev/null +++ b/PracticingAlgorithms/test_sorting.rb @@ -0,0 +1,14 @@ +require 'test/unit' +require 'sorting' + + +class TestSorting < Test::Unit::TestCase + def test_insertion_sort + tmpl = [8, 2, 4, 9, 3, 6] + a = Array.new(tmpl) + expected = [2, 3, 4, 6, 8, 9] + + insertion_sort(a) + assert_equal(expected, a) + end +end