Skip to content

Commit

Permalink
Add Python doctests to quicksort.py (MakeContributions#166)
Browse files Browse the repository at this point in the history
* Add Python doctests to quicksort.py

* quicksort(list(reversed(ascii_letters)))

* Update quick-sort.py
  • Loading branch information
cclauss authored Apr 12, 2021
1 parent 488eec3 commit 66c6538
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions sorting/python/quick-sort.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
def quickSort(arr):
n = len(arr)
if n == 1 or n == 0:
arr = [10, 1, 6, 256, 2, 53, 235, 53, 1, 7, 0, -23, 23]


def quicksort(arr):
"""
>>> quicksort(arr)
[-23, 0, 1, 1, 2, 6, 7, 10, 23, 53, 53, 235, 256]
>>> from string import ascii_letters
>>> quicksort(list(reversed(ascii_letters))) == sorted(ascii_letters)
True
"""
length = len(arr)
if length in (0, 1):
return arr
pi = 0
left = [arr[i] for i in range(n) if arr[i] <= arr[pi] and i != pi]
right = [arr[i] for i in range(n) if arr[i] > arr[pi]]
return quickSort(left) + [arr[pi]] + quickSort(right)
left = [arr[i] for i in range(length) if arr[i] <= arr[pi] and i != pi]
right = [arr[i] for i in range(length) if arr[i] > arr[pi]]
return quicksort(left) + [arr[pi]] + quicksort(right)


arr = [10, 1, 6, 256, 2, 53, 235, 53, 1, 7, 23]
print("Sorted Array:", quickSort(arr))
if __name__ == "__main__":
print("Sorted Array:", quicksort(arr))

0 comments on commit 66c6538

Please sign in to comment.