Add Python doctests to quicksort.py (#166)

* Add Python doctests to quicksort.py

* quicksort(list(reversed(ascii_letters)))

* Update quick-sort.py
pull/173/head
Christian Clauss 2021-04-12 18:22:12 +02:00 committed by GitHub
parent 488eec39ef
commit 66c65383fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 8 deletions

View File

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