Created
May 20, 2022 17:29
-
-
Save theabbie/46b673d6ea93544a7857c5dd5e06e84f to your computer and use it in GitHub Desktop.
Partial depth-limited Quick Sort Using range midpoints as partition
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
import random | |
def partition(arr, k): | |
n = len(arr) | |
i = -1 | |
for j in range(n): | |
if arr[j] <= k: | |
i += 1 | |
arr[i], arr[j] = arr[j], arr[i] | |
return i | |
def sort(arr, beg, end, d = 4): | |
if d == 0: | |
return | |
mid = (beg + end) // 2 | |
partition(arr, mid) | |
sort(arr, beg, mid, d - 1) | |
sort(arr, mid, end, d - 1) | |
arr = list(range(1000)) | |
random.shuffle(arr) | |
print(arr) | |
sort(arr, min(arr), max(arr), d = 9) | |
print(arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment