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.

This description is much closer to actual code, s<strong>in</strong>ce it is a better explanation of<br />

how to <strong>in</strong>sert the element A [i] <strong>in</strong>to the subarray that comes before it. It still uses<br />

an <strong>in</strong>formal description of mov<strong>in</strong>g elements if they are out of order, but this is not<br />

a terribly difficult th<strong>in</strong>g to do.<br />

A <strong>Java</strong> Description of Insertion-Sort<br />

Now we are ready to give <strong>Java</strong> code for this simple version of the <strong>in</strong>sertion-sort<br />

algorithm. We give such a description <strong>in</strong> Code Fragment 3.7 for the special case<br />

when A is an array of characters, a.<br />

Code Fragment 3.7: <strong>Java</strong> code for perform<strong>in</strong>g<br />

<strong>in</strong>sertion-sort on an array of characters.<br />

We illustrate an example run of the <strong>in</strong>sertion-sort algorithm <strong>in</strong> Figure 3.5.<br />

Figure 3.5: Execution of the <strong>in</strong>sertion-sort algorithm<br />

on an array of eight characters. We show the<br />

151

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

Saved successfully!

Ooh no, something went wrong!