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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

elements of S; that is, S 1 conta<strong>in</strong>s the first n/2 elements of S, <strong>and</strong> S 2 conta<strong>in</strong>s<br />

the rema<strong>in</strong><strong>in</strong>g n/2 elements.<br />

2. Recur: Recursively sort sequences S 1 <strong>and</strong> S 2 .<br />

3. Conquer: Put back the elements <strong>in</strong>to S by merg<strong>in</strong>g the sorted sequences S 1<br />

<strong>and</strong> S 2 <strong>in</strong>to a sorted sequence.<br />

In reference to the divide step, we recall that the notation x <strong>in</strong>dicates the<br />

ceil<strong>in</strong>g of x, that is, the smallest <strong>in</strong>teger m, such that x ≤ m. Similarly, the notation<br />

x <strong>in</strong>dicates the floor of x, that is, the largest <strong>in</strong>teger k, such that k ≤ x.<br />

We can visualize an execution of the merge-sort algorithm by means of a b<strong>in</strong>ary<br />

tree T, called the merge-sort tree. Each node of T represents a recursive<br />

<strong>in</strong>vocation (or call) of the merge-sort algorithm. We associate with each node v of<br />

T the sequence S that is processed by the <strong>in</strong>vocation associated with v. The<br />

children of node v are associated with the recursive calls that process the<br />

subsequences S 1 <strong>and</strong> S 2 of S. The external nodes of T are associated with<br />

<strong>in</strong>dividual elements of S, correspond<strong>in</strong>g to <strong>in</strong>stances of the algorithm that make no<br />

recursive calls.<br />

Figure 11.1 summarizes an execution of the merge-sort algorithm by show<strong>in</strong>g the<br />

<strong>in</strong>put <strong>and</strong> output sequences processed at each node of the merge-sort tree. The<br />

step-by-step evolution of the merge-sort tree is shown <strong>in</strong> Figures 11.2 through<br />

11.4.<br />

This algorithm visualization <strong>in</strong> terms of the merge-sort tree helps us analyze the<br />

runn<strong>in</strong>g time of the merge-sort algorithm. In particular, s<strong>in</strong>ce the size of the <strong>in</strong>put<br />

sequence roughly halves at each recursive call of merge-sort, the height of the<br />

merge-sort tree is about log n (recall that the base of log is 2 if omitted).<br />

Figure 11.1: Merge-sort tree T for an execution of<br />

the merge-sort algorithm on a sequence with 8<br />

elements: (a) <strong>in</strong>put sequences processed at each node<br />

of T; (b) output sequences generated at each node of<br />

T.<br />

679

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

Saved successfully!

Ooh no, something went wrong!