mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2024-11-15 17:30:52 +00:00
add quicksort
This commit is contained in:
parent
f0ee9d7b10
commit
e30581d576
32
chapter4/quicksort.py
Normal file
32
chapter4/quicksort.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
def quicksort(arr):
|
||||||
|
if len(arr) < 2:
|
||||||
|
return arr
|
||||||
|
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import random
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def quicksort(arr):
|
||||||
|
# base case. arr of length 0 or 1 don't need sorting, so return them as is
|
||||||
|
if len(arr) < 2:
|
||||||
|
return arr
|
||||||
|
|
||||||
|
pivot = arr[0]
|
||||||
|
|
||||||
|
# split arr into 3 parts, [less] | [pivot] | [greater]
|
||||||
|
less = [i for i in arr[1:] if i <= pivot]
|
||||||
|
greater = [i for i in arr[1:] if i > pivot]
|
||||||
|
|
||||||
|
return quicksort(less) + [pivot] + quicksort(greater)
|
||||||
|
|
||||||
|
|
||||||
|
LOWER = 0
|
||||||
|
UPPER = 100
|
||||||
|
SAMPLE_SIZE = 15
|
||||||
|
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
|
||||||
|
logging.debug(numbers)
|
||||||
|
print(quicksort(numbers))
|
Loading…
Reference in New Issue
Block a user