Skip to content

Instantly share code, notes, and snippets.

@xplorld
Last active December 17, 2018 23:30
Show Gist options
  • Save xplorld/457ef34292b080fd948b323f7469af69 to your computer and use it in GitHub Desktop.
Save xplorld/457ef34292b080fd948b323f7469af69 to your computer and use it in GitHub Desktop.
One-line sort algorithms. Haskellized python code.
'''
Haskellized python code
License: WTFPL
'''
qsort1 = (lambda arr:arr if (len(arr)<2) else qsort1(filter(lambda x:x <= arr[0],arr[1:])) + [arr[0]] + qsort1(filter(lambda x:x > arr[0],arr[1:])))
qsort2 = (lambda arr:arr if (len(arr)<2) else qsort2([x for x in arr[1:] if x <= arr[0]]) + [arr[0]] + qsort2([x for x in arr[1:] if x > arr[0]]))
bsort = (lambda arr:arr if (len(arr)<2) else (lambda mid:bsort(filter(lambda x:x <= mid,arr))+bsort(filter(lambda x:x > mid,arr)))((max(arr)+min(arr))/2))
merge = lambda a,a1,a2: a+a2 if not a1 else a+a1 if not a2 else merge(a+[a1[0]],a1[1:],a2) if a1[0] < a2[0] else merge(a+[a2[0]],a1,a2[1:])
msort = lambda a:a if (len(a)<2) else merge([],msort(a[:len(a)/2]),msort(a[len(a)/2:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment