23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

merge-sort, are implemented by either arrays or l<strong>in</strong>ked lists (the same as S), so that<br />

merg<strong>in</strong>g two sorted sequences can be done <strong>in</strong> l<strong>in</strong>ear time.<br />

As we mentioned earlier, we analyze the merge-sort algorithm by referr<strong>in</strong>g to the<br />

merge-sort tree T. (Recall Figures 11.2 through 11.4.) We call the time spent at a<br />

node v of T the runn<strong>in</strong>g time of the recursive call associated with v, exclud<strong>in</strong>g the<br />

time taken wait<strong>in</strong>g for the recursive calls associated with the children of v to<br />

term<strong>in</strong>ate. In other words, the time spent at node v <strong>in</strong>cludes the runn<strong>in</strong>g times of the<br />

divide <strong>and</strong> conquer steps, but excludes the runn<strong>in</strong>g time of the recur step. We have<br />

already observed that the details of the divide step are straightforward; this step runs<br />

<strong>in</strong> time proportional to the size of the sequence for v. In addition, as discussed<br />

above, the conquer step, which consists of merg<strong>in</strong>g two sorted subsequences, also<br />

takes l<strong>in</strong>ear time, <strong>in</strong>dependent of whether we are deal<strong>in</strong>g with arrays or l<strong>in</strong>ked lists.<br />

That is, lett<strong>in</strong>g i denote the depth of node v, the time spent at node v is O(n/2i),<br />

s<strong>in</strong>ce the size of the sequence h<strong>and</strong>led by the recursive call associated with v is<br />

equal to n/2i.<br />

Look<strong>in</strong>g at the tree T more globally, as shown <strong>in</strong> Figure 11.7, we see that, given our<br />

def<strong>in</strong>ition of "time spent at a node," the runn<strong>in</strong>g time of merge-sort is equal to the<br />

sum of the times spent at the nodes of T. Observe that T has exactly 2 i nodes at<br />

depth i. This simple observation has an important consequence, for it implies that<br />

the overall time spent at all the nodes of T at depth i is O(2 i • n/2i), which is O(n).<br />

By Proposition 11.1, the height of T is …logn…. Thus, s<strong>in</strong>ce the time spent at each<br />

of the …logn… + 1 levels of T is O(n), we have the follow<strong>in</strong>g result:<br />

Proposition 11.2: Algorithm merge-sort sorts a sequence S of size n <strong>in</strong><br />

O(nlogn) time, assum<strong>in</strong>g two elements of S can be compared <strong>in</strong> O(1) time.<br />

In other words, the merge-sort algorithm asymptotically matches the fast runn<strong>in</strong>g<br />

time of the heap-sort algorithm.<br />

Figure 11.7: A visual time analysis of the merge-sort<br />

tree T. Each node is shown labeled with the size of its<br />

subproblem.<br />

687

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!