Selection sorting.
Remember that selection sort works by
finding the smallest element and moving it into the first position,
then finding the second smallest and moving it into the second
position. In general, on the ith iteration, it finds the ith smallest
element and move it to position i. This works as follows:
(selection '(6 4 5 3 9 3))
(cons 3 (selection '(6 4 5 9 3))
(cons 3 (cons 3 (selection '(6 4 5 9))
(cons 3 (cons 3 (cons 4 (selection '(6 5 9))
(cons 3 (cons 3 (cons 4 (cons 5 (selection '(6 9))
(cons 3 (cons 3 (cons 4 (cons 5 (cons 6 (selection '(9))
(cons 3 (cons 3 (cons 4 (cons 5 (cons 6 (cons 9 (selection '())
Which becomes (3 3 4 5 6 9).
The scheme code below does exactly this:
def selection(L):
if len(L) == 0:
return []
else: # put the smallest element at the front of the current list
return [smallest(L,L[0])] + selection(myremove(L,smallest(L, L[0])))
# call selection on the list minus the smallest element
# remove the first occurance of atom A from L
def myremove(L, A):
L.remove(A)
return L
def smallest(L, A): # looks for the smallest element in the list
#atom A is the current smallest
if len(L)==0:
return A
elif L[0] < A:
return smallest(L[1:], L[0])
else:
return smallest(L[1:], A)
x = [6,3,4,9,10,1,7]
print x
print smallest(x, 6)
#print myremove(x, smallest(x,6))
print selection(x)
Merge Sort
Mergesort divides the list in half everytime,
calling itself on each half. As the recursion unwinds, it merges the
sorted list into a new
sorted list. For example, assume we have the following:
(mergesort '(6 4 5 7 8 9 3 4))
/ \
(mergesort '(6 4 5 7)) (mergesort '(8 9 3 4))
/ \ / \
(mergesort '(6 4)) (mergesort '(5 7)) (mergesort '(8 9)) (mergesort '(3 4))
At the lowest tree leves, the two element get sorted and the recursion unwinds.
(mergesort '(6 4 5 7 8 9 3 4))
/ \
(mergesort '(6 4 5 7)) (mergesort '(8 9 3 4))
/ \ / \
(4 6) (5 7) (8 9)) (3 4))
The next level of recursion:
(mergesort '(6 4 5 7 8 9 3 4))
/ \
(4 5 6 7)) (3 4 8 9)
/ \ / \
(4 6) (5 7) (8 9)) (3 4))
And the final level with merges into the overall list
(3 4 4 5 6 7 8 9)
/ \
(4 5 6 7) (3 4 8 9)
Of course, this is not the actual order that things happen but it give
the overall algorithm flavor.
In scheme, we can write functions to split lists, and merge sorted
lists. Once we have these, the mergesort itself is fairly short.
def mergesort(L):
if len(L) == 0:
return []
elif len(L) == 1:
return L
elif len(L) == 2:
return mergelists ([L[0]], L[1:])
else:
return mergelists(mergesort(split(L)[0]),mergesort(split(L)[1:][0]))
def mergelists(L, M): # assume L and M are sorted lists
if len(L)==0:
return M
elif len(M)==0:
return L
elif L[0] < M[0]:
return [L[0]] + mergelists(L[1:], M)
else:
return [M[0]] + mergelists(L, M[1:])
def sub(L, start, stop, ctr): # extract elements start to stop into a list
if len(L)== 0:
return L
elif ctr < start:
return sub(L[1:], start, stop, ctr+1)
elif ctr > stop:
return []
else:
return [L[0]] + sub(L[1:],start, stop, ctr+1)
def split(L): # split the list in half:, ; returns ((first half)(second half))
length=len(L)
if length == 0:
return [L]+[L]
elif length == 1:
return [L] + [[]]
else:
return [sub(L, 1, length/2, 1)] + [sub(L, (length/2)+1, length, 1)]
x = [6,3,4,9,10,1,7]
print x
#print split(x)
print mergesort(x)