Skip to content

Instantly share code, notes, and snippets.

@joshbode
Created March 16, 2013 15:09
Show Gist options
  • Select an option

  • Save joshbode/5176785 to your computer and use it in GitHub Desktop.

Select an option

Save joshbode/5176785 to your computer and use it in GitHub Desktop.
def insert_sort(l, inc=1):
"""Insert sort."""
for i, x in enumerate(l):
while x < l[i - inc] and i > 0:
l[i] = l[i - inc]
i -= inc
l[i] = x
def gby_gaps(n):
"""Gonnet & Baeza-Yates gaps."""
inc = n / 2
while True:
yield inc
inc = 1 if inc == 2 else int(5.0 * inc / 11)
if not inc:
break
def shell_sort(l, steps=gby_gaps):
"""Shell sort."""
for inc in steps(len(l)):
insert_sort(l, inc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment