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.

Figure 6.1: Array-based implementation of an array<br />

list S that is stor<strong>in</strong>g n elements: (a) shift<strong>in</strong>g up for an<br />

<strong>in</strong>sertion at <strong>in</strong>dex i(b); shift<strong>in</strong>g down for a removal at<br />

<strong>in</strong>dex i<br />

The Performance of a Simple Array-Based<br />

Implementation<br />

Table 6.2 shows the worst-case runn<strong>in</strong>g times of the methods of an array list with<br />

n elements realized by means of an array. Methods isEmpty, size, get <strong>and</strong><br />

set clearly run <strong>in</strong> O(1) time, but the <strong>in</strong>sertion <strong>and</strong> removal methods can take<br />

much longer than this. In particular, add(i, e) runs <strong>in</strong> time O(n). Indeed, the<br />

worst case for this operation occurs when i = 0, s<strong>in</strong>ce all the exist<strong>in</strong>g n elements<br />

318

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

Saved successfully!

Ooh no, something went wrong!