Mergesort+(ppalg+példa)

Az összefésüléses rendezés ppalg pszeudokódban leírt algoritmusa. code algorithm MergeSort is input arr: Array output sorted: Array var part1, part2: Array do if arr.size > 0 (part1,part2) := slice(arr) mergesort(part1) mergesort(part2) sorted := merge(part1,part2) end

function slice is input arr: Array output part1, part2: Array var middle, end: index of arr init middle, end := arr.first end += 1 do while end <= arr.last middle := middle + 1 end := end + 2 part1 := arr( arr.first .. middle ) part2 := arr( middle+1 .. arr.end ) end

function merge is input part1, part2: Array output arr: Array( part1.size + part2.size ) var pos: index of arr next1: index of part1 next2: index of part2 init arr := empty next1 := part1.first next2 := part2.first do while next1 < part1.end or next2 < part2.end if part1(next1) < part2(next2) or next2 = part2.end arr(pos) := part1(next1) next1 := next1 + 1 else arr(pos) := part2(next2) next2 := next2 + 1 pos := pos + 1

end

code