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.

These methods for add<strong>in</strong>g <strong>and</strong> remov<strong>in</strong>g objects <strong>in</strong> an array of high scores are<br />

simple. Nevertheless, they form the basis of techniques that are used repeatedly to<br />

build more sophisticated data structures. These other structures may be more<br />

general than the array structure above, of course, <strong>and</strong> often they will have a lot<br />

more operations that they can perform than just add <strong>and</strong> remove. But study<strong>in</strong>g<br />

the concrete array data structure, as we are do<strong>in</strong>g now, is a great start<strong>in</strong>g po<strong>in</strong>t to<br />

underst<strong>and</strong><strong>in</strong>g these other structures, s<strong>in</strong>ce every data structure has to be<br />

implemented us<strong>in</strong>g concrete means.<br />

In fact, later <strong>in</strong> this book, we will study a <strong>Java</strong> Collections Class, ArrayList,<br />

which is more general than the array structure we are study<strong>in</strong>g here. The<br />

ArrayList has methods to do a lot of the th<strong>in</strong>gs we will want to do with an<br />

array, while also elim<strong>in</strong>at<strong>in</strong>g the error that occurs when add<strong>in</strong>g an object to a full<br />

array. The ArrayList elim<strong>in</strong>ates this error by automatically copy<strong>in</strong>g the objects<br />

<strong>in</strong>to a larger array if necessary. Rather than discuss this process here, however, we<br />

will say more about how this is done when we discuss the ArrayList <strong>in</strong> detail.<br />

3.1.2 Sort<strong>in</strong>g an Array<br />

In the previous section, we worked hard to show how we can add or remove objects<br />

at a certa<strong>in</strong> <strong>in</strong>dex i <strong>in</strong> an array while keep<strong>in</strong>g the previous order of the objects <strong>in</strong>tact.<br />

In this section, we study a way of start<strong>in</strong>g with an array with objects that are out of<br />

order <strong>and</strong> putt<strong>in</strong>g them <strong>in</strong> order. This is known as the sort<strong>in</strong>g problem.<br />

A Simple Insertion-Sort Algorithm<br />

We study several sort<strong>in</strong>g algorithms <strong>in</strong> this book, most of which appear <strong>in</strong> Chapter<br />

11. As a warm up, we describe <strong>in</strong> this section a nice, simple sort<strong>in</strong>g algorithm<br />

called <strong>in</strong>sertion–sort. In this case, we describe a specific version of the algorithm<br />

where the <strong>in</strong>put is an array of comparable elements. We consider more general<br />

k<strong>in</strong>ds of sort<strong>in</strong>g algorithms later <strong>in</strong> this book.<br />

149

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

Saved successfully!

Ooh no, something went wrong!