Skip to content

Instantly share code, notes, and snippets.

@nathanielbd
Created December 30, 2020 20:33
Show Gist options
  • Save nathanielbd/d443ad1ea11abd14a47bfcfebed6c25c to your computer and use it in GitHub Desktop.
Save nathanielbd/d443ad1ea11abd14a47bfcfebed6c25c to your computer and use it in GitHub Desktop.
A reluctant sorting algorithm
# https://www.mipmip.org/tidbits/pasa.pdf
# Pessimal Algorithmsand Simplexity Analysis. Broder, Stolfi
def helper(A, i, j):
# in-place sort the subarray A[i],...,A[j] using multiply-and-surrender
if i >= j:
return
m = (i+j)//2
helper(A, i, m)
helper(A, m+1, j)
if A[m] > A[j]:
A[m],A[j] = A[j],A[m]
helper(A, i, j-1)
# start recursion and return the sorted array
def slowsort(A):
helper(A, 0, len(A)-1)
return A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment