Add 'PracticingAlgorithms/' from commit '0b723f2ae0a20ab8c90c33db8c1d9d9acd76a86f'

git-subtree-dir: PracticingAlgorithms
git-subtree-mainline: 0a9428093c
git-subtree-split: 0b723f2ae0
This commit is contained in:
Dan Buch 2013-01-09 23:58:26 -05:00
commit eacc351b17
11 changed files with 247 additions and 0 deletions

2
PracticingAlgorithms/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin/*
*.o

View File

@ -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

View File

@ -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!
- <a href="http://videolectures.net/mit6046jf05_leiserson_lec01/">Intro. to Algorithms lecture 01</a>

10
PracticingAlgorithms/bench Executable file
View File

@ -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)"

View File

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,8 @@
all: $(BINDIR)/insertion-sort
$(BINDIR)/insertion-sort: insertion-sort.c
gcc -o $@ $(CFLAGS) $^
.PHONY: all

View File

@ -0,0 +1,38 @@
#include <stdio.h>
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
*/

View File

@ -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()

View File

@ -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