Add Python doctests to quicksort.py (#166)
* Add Python doctests to quicksort.py * quicksort(list(reversed(ascii_letters))) * Update quick-sort.pypull/173/head
parent
488eec39ef
commit
66c65383fb
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue