Created
September 23, 2019 10:31
-
-
Save yuckiymouse/dc8bf99fe7af2411fd85f2bee36a90c9 to your computer and use it in GitHub Desktop.
quick sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def quick_sort(arr): | |
| # make 2 groups | |
| left = [] | |
| right = [] | |
| if len(arr) <= 1: | |
| return arr | |
| # pick random number as pivot | |
| ref = random.choice(arr) | |
| for ele in arr: | |
| if ele < ref: | |
| left.append(ele) | |
| elif ele > ref: | |
| right.append(ele) | |
| else: | |
| ref_count += 1 | |
| left = quick_sort(left) | |
| right = quick_sort(right) | |
| return left + [ref] * ref_count + right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment