Skip to content

Instantly share code, notes, and snippets.

@paulhankin
Created May 25, 2016 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulhankin/a00eb9948834269f3111fa2e0f435a08 to your computer and use it in GitHub Desktop.
Save paulhankin/a00eb9948834269f3111fa2e0f435a08 to your computer and use it in GitHub Desktop.
import itertools
import random
def power_two(x):
return x and (x & (x - 1) == 0)
def asort(A, i, j):
assert power_two(j - i + 1), '%d - %d + 1 not power of 2' % (j, i)
if A[i-1] > A[j-1]:
A[i-1], A[j-1] = A[j-1], A[i-1]
if i + 1 < j:
k = (j - i + 1) // 4
asort(A, i, j - 2*k)
asort(A, j - 2*k + 1, j)
asort(A, i + k, j - k)
asort(A, i, j - 2*k)
asort(A, j - 2*k + 1, j)
asort(A, i + k, j - k)
N = 16
for B in [(11,21,13,41,15,16,71,28,19,10,1,12,3,14,5,6)]:
A = list(B)
asort(A, 1, len(A))
if A != sorted(B):
print 'failed on', B
print 'got', A
assert False
print B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment