Skip to content

Commit 6e8f242

Browse files
Ryan PonRyan Pon
authored andcommitted
Initial code dump.
1 parent 9ac8a7a commit 6e8f242

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

bubble_sort.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
A practice implementation of bubble sort.
3+
"""
4+
5+
def non_increasing(a, b):
6+
""" Returns true if the elements are in non-increasing order. """
7+
return a >= b
8+
9+
def non_decreasing(a, b):
10+
""" Returns true if the elements are in non-decreasing order. """
11+
return a <= b
12+
13+
def bubble_sort(input, compare=non_decreasing):
14+
"""
15+
The classic bubble sort algorithm.
16+
17+
Arguments:
18+
input -- the list of numbers to be sorted
19+
20+
Keyword Arguments:
21+
compare(a, b) -- function, returns true if the two elements are in order
22+
23+
>>> example = [9, 1, 4, 7, 2]
24+
>>> bubble_sort(example, non_increasing)
25+
[9, 7, 4, 2, 1]
26+
>>> bubble_sort(example)
27+
[1, 2, 4, 7, 9]
28+
"""
29+
list_len = len(input)
30+
for num_sorted in xrange(list_len - 1):
31+
# subtract num_sorted from inner loop -- end is already sorted
32+
for i in xrange(0, list_len - num_sorted - 1):
33+
# test if the element should be swapped with next element
34+
if not compare(input[i], input[i+1]):
35+
input[i], input[i+1] = input[i+1], input[i]
36+
return input
37+
38+
if __name__ == "__main__":
39+
# a check to make sure that docs are up to date
40+
import doctest
41+
doctest.testmod()

insertion_sort.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
A practice implementation of insertion sort.
3+
"""
4+
5+
def insertion_sort(array):
6+
"""
7+
Basic insertion sort algorithm.
8+
9+
Arguments:
10+
array -- the list of numbers to be sorted
11+
12+
>>> example = [9, 1, 4, 7, 2]
13+
>>> insertion_sort(example)
14+
[1, 2, 4, 7, 9]
15+
"""
16+
for i in xrange(1, len(array)):
17+
val = array[i]
18+
hole = i
19+
while hole != 0 and val < array[hole-1]:
20+
array[hole] = array[hole-1]
21+
hole -= 1
22+
array[hole] = val
23+
return array
24+
25+
if __name__ == "__main__":
26+
# a check to make sure that docs are up to date
27+
import doctest
28+
doctest.testmod()

merge_sort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
def merge_sort(array):
4+
if len(array) < 2:
5+
return array
6+
mid = len(array) / 2
7+
left = merge_sort(array[:mid])
8+
right = merge_sort(array[mid:])
9+
return merge(left, right)
10+
11+
def merge(left, right):
12+
result = []
13+
while left and right:
14+
if left[0] <= right[0]:
15+
result.append(left.pop(0))
16+
else:
17+
result.append(right.pop(0))
18+
result.extend(right)
19+
result.extend(left)
20+
return result
21+
22+
if __name__ == '__main__':
23+
from random import randint
24+
rng = 100000
25+
test = [randint(0, rng) for _ in xrange(rng)]
26+
assert merge_sort(test) == sorted(test)

quicksort.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
from random import randint
3+
4+
def quicksort(array):
5+
array_length = len(array)
6+
if array_length < 2:
7+
return array
8+
left = []
9+
right = []
10+
pivot = array.pop(randint(0, array_length-1))
11+
while array:
12+
val = array.pop()
13+
if val > pivot:
14+
right.append(val)
15+
else:
16+
left.append(val)
17+
return quicksort(left) + [pivot] + quicksort(right)
18+
19+
if __name__ == '__main__':
20+
print quicksort([1,7,3,9,2])

selection_sort.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
A practice implementation of selection sort.
3+
"""
4+
5+
def gt(a, b):
6+
return a > b
7+
8+
def lt(a, b):
9+
return a < b
10+
11+
def selection_sort(input, compare=lt):
12+
"""
13+
Basic selection sort algorithm.
14+
15+
Arguments:
16+
input -- the list of numbers to be sorted
17+
18+
Keyword Arguments:
19+
compare(a, b) -- function, returns true if the two elements are in order
20+
21+
>>> example = [9, 1, 4, 7, 2]
22+
>>> selection_sort(example, gt)
23+
[9, 7, 4, 2, 1]
24+
>>> selection_sort(example)
25+
[1, 2, 4, 7, 9]
26+
"""
27+
list_len = len(input)
28+
for i in xrange(list_len - 1):
29+
# the index where the target, based on compare, resides
30+
target_index = i
31+
for j in xrange(i+1, list_len):
32+
if not compare(input[target_index], input[j]):
33+
target_index = j
34+
# swap the target to the end of the sorted list
35+
input[i], input[target_index] = input[target_index], input[i]
36+
return input
37+
38+
if __name__ == "__main__":
39+
# a check to make sure that docs are up to date
40+
import doctest
41+
doctest.testmod()

0 commit comments

Comments
 (0)