Skip to content

Instantly share code, notes, and snippets.

@theabbie
Created May 20, 2022 17:29
Show Gist options
  • Save theabbie/46b673d6ea93544a7857c5dd5e06e84f to your computer and use it in GitHub Desktop.
Save theabbie/46b673d6ea93544a7857c5dd5e06e84f to your computer and use it in GitHub Desktop.
Partial depth-limited Quick Sort Using range midpoints as partition
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