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.

there is no swap needed, <strong>and</strong> returns back to the outer loop. That is, we perform<br />

only one iteration of the <strong>in</strong>ner loop for each iteration of the outer loop. Thus, <strong>in</strong><br />

this case, we perform a m<strong>in</strong>imum number of comparisons. Of course, we might<br />

have to do a lot more work than this if the <strong>in</strong>put array is extremely out of order. In<br />

fact, we will have to do the most work if the <strong>in</strong>put array is <strong>in</strong> decreas<strong>in</strong>g order.<br />

3.1.3 java.util Methods for Arrays <strong>and</strong> R<strong>and</strong>om Numbers<br />

Because arrays are so important, <strong>Java</strong> provides a number of built-<strong>in</strong> methods for<br />

perform<strong>in</strong>g common tasks on arrays. These methods appear as static methods <strong>in</strong> the<br />

java.util.Arrays class. That is, they are associated with the class,<br />

java.util.Arrays itself, <strong>and</strong> not with a particular <strong>in</strong>stance of this class.<br />

Describ<strong>in</strong>g some of these methods will have to wait, however, until later <strong>in</strong> this<br />

book (when we discuss the concept that these methods are based on).<br />

Some Simple Methods of java.util.Arrays<br />

We list below some simple methods of class java.util.Arrays that need no<br />

further explanation:<br />

equals(A, B): Returns true if <strong>and</strong> only if the array A <strong>and</strong> the array B<br />

are equal. Two arrays are considered equal if they have the same number of<br />

elements <strong>and</strong> every correspond<strong>in</strong>g pair of elements <strong>in</strong> the two arrays are equal.<br />

That is, A <strong>and</strong> B have the same elements <strong>in</strong> the same order.<br />

fill(A,x): Stores element x <strong>in</strong>to every cell of array A.<br />

elements.<br />

sort(A): Sorts the array A us<strong>in</strong>g the natural order<strong>in</strong>g of its<br />

toStr<strong>in</strong>g(A): Returns a Str<strong>in</strong>g representation of the array A.<br />

For example, the follow<strong>in</strong>g str<strong>in</strong>g would be returned by the method toStr<strong>in</strong>g<br />

called on an array of <strong>in</strong>tegers A = [4,5,2,3,5,7,10]:<br />

[4, 5, 2, 3, 5, 7, 10]<br />

Note that, from the list above, <strong>Java</strong> has a built-<strong>in</strong> sort<strong>in</strong>g algorithm. This is not the<br />

<strong>in</strong>sertion-sort algorithm we presented above, however. It is an algorithm called<br />

quick-sort, which usually runs much faster than <strong>in</strong>sertion—sort. We discuss the<br />

quick-sort algorithm <strong>in</strong> Section 11.2.<br />

An Example Us<strong>in</strong>g Pseudo-R<strong>and</strong>om Numbers<br />

153

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

Saved successfully!

Ooh no, something went wrong!