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.

Analyz<strong>in</strong>g R<strong>and</strong>omized Quick-Select.........<br />

531<br />

11.8<br />

Exercises.........................<br />

532<br />

java.datastructures.net<br />

11.1 Merge-Sort<br />

In this section, we present a sort<strong>in</strong>g technique, called merge-sort, which can be<br />

described <strong>in</strong> a simple <strong>and</strong> compact way us<strong>in</strong>g recursion.<br />

11.1.1 Divide-<strong>and</strong>-Conquer<br />

Merge-sort is based on an algorithmic design pattern called divide-<strong>and</strong>-conquer.<br />

The divide-<strong>and</strong>-conquer pattern consists of the follow<strong>in</strong>g three steps:<br />

1. Divide: If the <strong>in</strong>put size is smaller than a certa<strong>in</strong> threshold (say, one or two<br />

elements), solve the problem directly us<strong>in</strong>g a straightforward method <strong>and</strong> return<br />

the solution so obta<strong>in</strong>ed. Otherwise, divide the <strong>in</strong>put data <strong>in</strong>to two or more<br />

disjo<strong>in</strong>t subsets.<br />

2. Recur: Recursively solve the subproblems associated with the subsets.<br />

3. Conquer: Take the solutions to the subproblems <strong>and</strong> "merge" them <strong>in</strong>to a<br />

solution to the orig<strong>in</strong>al problem.<br />

Us<strong>in</strong>g Divide-<strong>and</strong>-Conquer for Sort<strong>in</strong>g<br />

Recall that <strong>in</strong> a sort<strong>in</strong>g problem we are given a sequence of n objects, stored <strong>in</strong> a<br />

l<strong>in</strong>ked list or an array, together with some comparator def<strong>in</strong><strong>in</strong>g a total order on<br />

these objects, <strong>and</strong> we are asked to produce an ordered representation of these<br />

objects. To allow for sort<strong>in</strong>g of either representation, we will describe our sort<strong>in</strong>g<br />

algorithm at a high level for sequences <strong>and</strong> expla<strong>in</strong> the details needed to<br />

implement it for l<strong>in</strong>ked lists <strong>and</strong> arrays. To sort a sequence S with n elements<br />

us<strong>in</strong>g the three divide-<strong>and</strong>-conquer steps, the merge-sort algorithm proceeds as<br />

follows:<br />

1. Divide:If S has zero or one element, return S immediately; it is already<br />

sorted. Otherwise (S has at least two elements), remove all the elements from S<br />

<strong>and</strong> put them <strong>in</strong>to two sequences, S 1 <strong>and</strong> S 2 , each conta<strong>in</strong><strong>in</strong>g about half of the<br />

678

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

Saved successfully!

Ooh no, something went wrong!