Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
The Merge method of the TwoWayMergeSorter class is defined in Program . Altogether, this method takes three integer parameters, left, middle, and right. It is assumed that
Furthermore, it is assumed that the two subsequences of the array,
and
are both sorted. The Merge method merges the two sorted subsequences using the temporary array, tempArray. It then copies the merged (and sorted) sequence into the array at
Program: TwoWayMergeSorter class Merge method.
In order to determine the running time of the Merge method it is necessary to recognize that the total number of iterations of the two loops (lines 10-16, lines 17-18) is , in the worst case. The total number of iterations of the third loop (lines 19-20) is the same. Since all the loop bodies do a constant amount of work, the total running time for the Merge method is O(n), where is the total number of elements in the two subsequences that are merged.