Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''Rearrange list around the pivot such that elements less than the pivot A[i]
- are placed to the left and elements gretaer than the pivot to the right of
- the pivot. Book Elements of programming interviews in Py (2018) p.44
- Hint: Think about the partition step in quicksort. Duch national flag
- partitioning method..
- '''
- A = [4,1,2,11,2,3,5,7,3,4,1,6]
- def rearrange(pivot):
- right = (i for i in A if i > A[pivot])
- left = (i for i in A if i < A[pivot])
- middle = (i for i in A if i == A[pivot])
- return (left, middle, right)
- r = rearrange(2)
- for partition in r:
- for i in r[partition]:
- print(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement