all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,8 @@
|
|||
def insertion_sort(l):
|
||||
for i in xrange(1, len(l)):
|
||||
j = i-1
|
||||
key = l[i]
|
||||
while (l[j] > key) and (j >= 0):
|
||||
l[j+1] = l[j]
|
||||
j -= 1
|
||||
l[j+1] = key
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
def insertion_sort_bin(seq):
|
||||
for i in range(1, len(seq)):
|
||||
key = seq[i]
|
||||
# invariant: ``seq[:i]`` is sorted
|
||||
# find the least `low' such that ``seq[low]`` is not less then `key'.
|
||||
# Binary search in sorted sequence ``seq[low:up]``:
|
||||
low, up = 0, i
|
||||
while up > low:
|
||||
middle = (low + up) // 2
|
||||
if seq[middle] < key:
|
||||
low = middle + 1
|
||||
else:
|
||||
up = middle
|
||||
# insert key at position ``low``
|
||||
seq[:] = seq[:low] + [key] + seq[low:i] + seq[i + 1:]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import bisect
|
||||
def insertion_sort_bin(seq):
|
||||
for i in range(1, len(seq)):
|
||||
bisect.insort(seq, seq.pop(i), 0, i)
|
||||
Loading…
Add table
Add a link
Reference in a new issue