Name: Anonymous 2011-12-10 2:12
Unless you want your programs to segfault, then it's okay.
MERGE_SORT(array A)
if (A is empty)
return the empty array.
else
return MERGE(MERGE_SORT(left half of A), MERGE_SORT(right half of A))
MERGE(A,B)
has been proven to generate a sorted array if A
and B
are both sorted. With this, you prove that MERGE_SORT
is correct, by induction on the length of A
.A
is zero.A
is one, then an array of its only elements is returned, and any array of length one is sorted.n > 1
and suppose that MERGE_SORT
returns a sorted array, for all input arrays that have length less than n
, and let A
be an array of length n
. The left and right half of A
must both have length strictly less than n
. So MERGE_SORT
applied to each half will yield two sorted arrays. MERGE
is then applied to two sorted arrays, and will yield a sorted array, which is then returned from MERGE_SORT(A)
. By induction, we are done.