def bubble_sort(l):
for i in range(len(l),0,-1):
for j in range(len(l)-1):
if l[j] > l[j+1]:
tmp = l[j]
l[j] = l[j+1]
l[j+1] = tmp
l = [3,5,1,1,2,5,3,4,7]
bubble_sort(l)
print l
def insertion_sort(a):
for i in range(len(a)):
index = i
for j in range(i+1,len(a)):
if a[j] < a[index]:
index = j
tmp = a[i]
a[i] = a[index]
a[index] = tmp
a = [3,5,2,2,1,4,5,7,-1,0,99,8976789659,897689,75,6]
insertion_sort(a)
print a
def merge(a, b):
c = []
h = j = 0
while j < len(a) and h < len(b):
if a[j] < b[h]:
c.append(a[j])
j += 1
else:
c.append(b[h])
h += 1
if j == len(a):
for i in b[h:]:
c.append(i)
else:
for i in a[j:]:
c.append(i)
return c
def merge_sort(lists):
if len(lists) <= 1:
return lists
middle = len(lists)/2
left = merge_sort(lists[:middle])
right = merge_sort(lists[middle:])
return merge(left, right)
l = [23,3,5,346,23546,25461,12345546,1234]
merge_sort(l)
def qsort(L):
if len(L) <= 1: return L
return qsort([lt for lt in L[1:] if lt < L[0]]) + [L[0]] + qsort([ge for ge in L[1:] if ge >= L[0]])
l = [23,3,5,346,23546,25461,12345546,1234]
qsort(l)