Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
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

displaymath69169

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

displaymath69170

and

displaymath69171

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

displaymath69172

   program44040
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 11-17, lines 18-19) is tex2html_wrap_inline69177, in the worst case. The total number of iterations of the third loop (lines 20-21) 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_inline69181 is the total number of elements in the two subsequences that are merged.


next up previous contents index

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