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.

completed (sorted) part of the array <strong>in</strong> white, <strong>and</strong> we<br />

color the next element that is be<strong>in</strong>g <strong>in</strong>serted <strong>in</strong>to the<br />

sorted part of the array with light blue. We also<br />

highlight that character on the left, s<strong>in</strong>ce it is stored <strong>in</strong><br />

the cur variable. Each row corresponds to an iteration<br />

of the outer loop, <strong>and</strong> each copy of the array <strong>in</strong> a row<br />

corresponds to an iteration of the <strong>in</strong>ner loop. Each<br />

comparison is shown with an arc. In addition, we<br />

<strong>in</strong>dicate whether that comparison resulted <strong>in</strong> a move<br />

or not.<br />

An <strong>in</strong>terest<strong>in</strong>g th<strong>in</strong>g happens <strong>in</strong> the <strong>in</strong>sertion-sort algorithm if the array is already<br />

sorted. In this case, the <strong>in</strong>ner loop does only one comparison, determ<strong>in</strong>es that<br />

152

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

Saved successfully!

Ooh no, something went wrong!