CP164 LECTURE 15 NOTES
The Merge Sort
This algorithm uses the divide and conquer approach which is an important
algorithmic technique in Computer Science. This algorithm splits an array up
into two smaller arrays, recursively sorts the two smaller arrays, and
finally merges the smaller arrays back into the big array. Merge sort is a stable
sort.
Merge Sort Algorithm
The merge sort algorithm works as follows. If n=1, there is only one element to
sort, and we are done. Otherwise it divides the array into two halves, and
recursively applies merge sort on the first half and on the second half. This
gives two sorted halves, which can then be merged together.
The main operation in this algorithm is merging the two sorted arrays. The
following figure shows the process of merging two sorted arrays. The merging
algorithm takes two sorted input arrays and one output array. It traverses the
input arrays, copying the smaller of the two into the output array in each
iteration. When either input array is copied completely to the output, the
remainder of the other array is copied to the output array.
, Merging Two Sorted
The Merge Sort
This algorithm uses the divide and conquer approach which is an important
algorithmic technique in Computer Science. This algorithm splits an array up
into two smaller arrays, recursively sorts the two smaller arrays, and
finally merges the smaller arrays back into the big array. Merge sort is a stable
sort.
Merge Sort Algorithm
The merge sort algorithm works as follows. If n=1, there is only one element to
sort, and we are done. Otherwise it divides the array into two halves, and
recursively applies merge sort on the first half and on the second half. This
gives two sorted halves, which can then be merged together.
The main operation in this algorithm is merging the two sorted arrays. The
following figure shows the process of merging two sorted arrays. The merging
algorithm takes two sorted input arrays and one output array. It traverses the
input arrays, copying the smaller of the two into the output array in each
iteration. When either input array is copied completely to the output, the
remainder of the other array is copied to the output array.
, Merging Two Sorted