Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Merging

The Merge method of the TwoWayMergeSorter class is defined in Program gif. Altogether, this method takes three integer parameters, left, middle, and right. It is assumed that

displaymath69452

Furthermore, it is assumed that the two subsequences of the array,

displaymath69453

and

displaymath69454

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

displaymath69455

   program44291
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 tex2html_wrap_inline69460, 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 tex2html_wrap_inline69464 is the total number of elements in the two subsequences that are merged.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.