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:
(* put the smallest element at the front of the current list
call selection on the list minus the smallest element *)
fun selectionsort([]) = []
| selectionsort(first::rest) =
let
val s = smallest(rest, first);
in
s::selectionsort(remove(first::rest,s))
end;
(* remove the first occurance of atom A from L *)
fun remove([], _) = []
| remove(first::rest, item) =
if first = item then
rest
else
first::remove(rest, item);
(* looks for the smallest element in the list
atom A is the current smallest *)
fun smallest([], a) = a
| smallest(first::rest, a) =
if first < a then
smallest(rest, first)
else smallest(rest, a);
(*
- use "selection.sml";
val it = [1,2,3,5] : int list
- remove([1,2,3,4,5], 5);
val it = [1,2,3,4] : int list
- remove([1,2,3,4,5], 1);
val it = [2,3,4,5] : int list
- smallest([3,14,5,1,18,2], 3);
val it = 1 : int
*)
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.
fun count([]) = 0
| count(first::rest) = 1 + count(rest);
fun sub(lis: int list, start, stop, ctr) =
if lis = nil then lis
else if ctr < start then
sub(tl(lis), start, stop, ctr+1)
else if ctr > stop then
[]
else
hd(lis)::sub(tl(lis),start, stop, ctr+1);
fun split(lis: int list) =
let
val len = count(lis)
in
if len = 0 then lis::[lis]
else if len = 1 then lis::[[]]
else sub(lis, 1, len div 2, 1)::[sub(lis, (len div 2)+1, len, 1)]
end;
fun mergelists(lis: int list, m:int list) =
if lis = nil then
m
else if m = nil then
lis
else if hd(lis) < hd(m) then
hd(lis)::mergelists(tl(lis), m)
else
hd(m)::mergelists(lis, tl(m));
fun mergesort(lis : int list) =
if lis = nil then
[]
else if tl(lis) = nil then
lis
else if tl(tl(lis)) = nil then
mergelists ([hd(lis)], tl(lis))
else
mergelists(mergesort(hd(split(lis))),
mergesort(hd(tl(split(lis)))));
(*
- use "mergesort.sml";
- mergesort([34,1,7,18,90, 2,0]);
val it = [0,1,2,7,18,34,90] : int list
-
*)