Skip to content

Commit

Permalink
use is_sorted assertion in all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Jan 28, 2020
1 parent 8f40838 commit eecc30e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
15 changes: 12 additions & 3 deletions algs4/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,20 @@ def sort(cls, arr):

return arr

@classmethod
def is_sorted(cls, arr):
for i in range(1, len(arr)):
if arr[i] < arr[i-1]:
return False
return True


if __name__ == '__main__':
import sys

items = []
for line in sys.stdin:
items = line.split()
print(' items: ', items)
print('sort items: ', Heap.sort(items))
items.extend(line.split())
print(' items: ', items)
print('sort items: ', Heap.sort(items))
assert Heap.is_sorted(items)
15 changes: 12 additions & 3 deletions algs4/insertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ def sort(cls, arr):
j -= 1
return arr

@classmethod
def is_sorted(cls, arr):
for i in range(1, len(arr)):
if arr[i] < arr[i-1]:
return False
return True


if __name__ == '__main__':
import sys

items = []
for line in sys.stdin:
items = line.split()
print(' items: ', items)
print('sort items: ', Insertion.sort(items))
items.extend(line.split())
print(' items: ', items)
print('sort items: ', Insertion.sort(items))
assert Insertion.is_sorted(items)
17 changes: 13 additions & 4 deletions algs4/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def mergesort(cls, arr, lo, hi):
if lo >= hi:
return

mid = (lo + hi) / 2
mid = (lo + hi) // 2
cls.mergesort(arr, lo, mid)
cls.mergesort(arr, mid + 1, hi)
cls.merge(arr, lo, mid, hi)
Expand All @@ -61,11 +61,20 @@ def mergesort(cls, arr, lo, hi):
def sort(cls, arr):
return cls.mergesort(arr, 0, len(arr) - 1)

@classmethod
def is_sorted(cls, arr):
for i in range(1, len(arr)):
if arr[i] < arr[i-1]:
return False
return True


if __name__ == '__main__':
import sys

items = []
for line in sys.stdin:
items = line.split()
print(' items: ', items)
print('sort items: ', Merge.sort(items))
items.extend(line.split())
print(' items: ', items)
print('sort items: ', Merge.sort(items))
assert Merge.is_sorted(items)
15 changes: 12 additions & 3 deletions algs4/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ def sort(cls, arr):
arr[i], arr[minIndex] = arr[minIndex], arr[i]
return arr

@classmethod
def is_sorted(cls, arr):
for i in range(1, len(arr)):
if arr[i] < arr[i-1]:
return False
return True


if __name__ == '__main__':
import sys

items = []
for line in sys.stdin:
items = line.split()
print(' items: ', items)
print('sort items: ', Selection.sort(items))
items.extend(line.split())
print(' items: ', items)
print('sort items: ', Selection.sort(items))
assert Selection.is_sorted(items)
23 changes: 16 additions & 7 deletions algs4/shell.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Sorts a sequence of strings from standard input using shell sort.
% more tiny.txt
S O R T E X A M P L E
Expand All @@ -10,7 +10,7 @@
A E E L M O P R S T X [ one string per line ]
% more words3.txt
Expand All @@ -29,7 +29,7 @@ class Shell:
def sort(cls, arr):
N = len(arr)
h = 1
while h < N / 3:
while h < N // 3:
h = 3 * h + 1 # 1, 4, 13, 40...

while h >= 1:
Expand All @@ -40,14 +40,23 @@ def sort(cls, arr):
break
arr[j], arr[j - h] = arr[j - h], arr[j]
j -= h
h /= 3
h //= 3
return arr

@classmethod
def is_sorted(cls, arr):
for i in range(1, len(arr)):
if arr[i] < arr[i-1]:
return False
return True


if __name__ == '__main__':
import sys

items = []
for line in sys.stdin:
items = line.split()
print(' items: ', items)
print('sort items: ', Shell.sort(items))
items.extend(line.split())
print(' items: ', items)
print('sort items: ', Shell.sort(items))
assert Shell.is_sorted(items)

0 comments on commit eecc30e

Please sign in to comment.