Merge Sort
function mergesort(m)
var list left, right, result
if length(m) ≤ 1
return m
else
var middle = length(m)
for each x in m up to middle
add x to left
for each x in m after middle
add x to right
left = mergesort(left)
right = mergesort(right)
result = merge(left, right)
return result
------
function merge(left,right)
var list result
while length(left) > 0 and length(right) > 0
if first(left) ≤ first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
if length(left) > 0
append rest(left) to result
if length(right) > 0
append rest(right) to result
return result
Tuesday, February 5, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment