Skip to content

Commit

Permalink
add is_sorted for assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Jan 27, 2020
1 parent 8b1dd0d commit a620f66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions algs4/quick.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ def quicksort(cls, arr, lo, hi):
def sort(cls, arr):
return cls.quicksort(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
Expand All @@ -69,3 +76,4 @@ def sort(cls, arr):
items.extend(line.split())
print(' items: ', items)
print('sort items: ', Quick.sort(items))
assert Quick.is_sorted(items)
8 changes: 8 additions & 0 deletions algs4/quick_3way.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def quicksort(cls, arr, lo, hi):
def sort(cls, arr):
return cls.quicksort(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
Expand All @@ -60,3 +67,4 @@ def sort(cls, arr):
items.extend(line.split())
print(' items: ', items)
print('sort items: ', Quick3Way.sort(items))
assert Quick3Way.is_sorted(items)

0 comments on commit a620f66

Please sign in to comment.