12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

30 Chap. 2 Mathematical Preliminarieswhile n! grows slower than n n (because √ 2πn/e n < 1), it grows faster than c n forany positive integer constant c.Permutations: A permutation of a sequence S is simply the members of S arrangedin some order. For example, a permutation of the integers 1 through nwould be those values arranged in some order. If the sequence contains n distinctmembers, then there are n! different permutations for the sequence. This is becausethere are n choices for the first member in the permutation; for each choice of firstmember there are n − 1 choices for the second member, <strong>and</strong> so on. Sometimesone would like <strong>to</strong> obtain a r<strong>and</strong>om permutation for a sequence, that is, one of then! possible permutations is selected in such a way that each permutation has equalprobability of being selected. A simple Java function for generating a r<strong>and</strong>om permutationis as follows. Here, the n values of the sequence are s<strong>to</strong>red in positions 0through n − 1 of array A, function swap(A, i, j) exchanges elements i <strong>and</strong>j in array A, <strong>and</strong> R<strong>and</strong>om(n) returns an integer value in the range 0 <strong>to</strong> n − 1 (seethe Appendix for more information on swap <strong>and</strong> R<strong>and</strong>om).// R<strong>and</strong>omly permute the values of array "A"static void permute(E[] A) {for (int i = A.length; i > 0; i--) // for each iswap(A, i-1, DSutil.r<strong>and</strong>om(i)); // swap A[i-1] with} // a r<strong>and</strong>om elementBoolean variables: A Boolean variable is a variable (of type boolean in Java)that takes on one of the two values true <strong>and</strong> false. These two values are oftenassociated with the values 1 <strong>and</strong> 0, respectively, although there is no reason whythis needs <strong>to</strong> be the case. It is poor programming practice <strong>to</strong> rely on the correspondencebetween 0 <strong>and</strong> false, because these are logically distinct objects ofdifferent types.Floor <strong>and</strong> ceiling: The floor of x (written ⌊x⌋) takes real value x <strong>and</strong> returns thegreatest integer ≤ x. For example, ⌊3.4⌋ = 3, as does ⌊3.0⌋, while ⌊−3.4⌋ = −4<strong>and</strong> ⌊−3.0⌋ = −3. The ceiling of x (written ⌈x⌉) takes real value x <strong>and</strong> returnsthe least integer ≥ x. For example, ⌈3.4⌉ = 4, as does ⌈4.0⌉, while ⌈−3.4⌉ =⌈−3.0⌉ = −3.Modulus opera<strong>to</strong>r: The modulus (or mod) function returns the remainder of aninteger division. Sometimes written n mod m in mathematical expressions, thesyntax for the Java modulus opera<strong>to</strong>r is n % m. From the definition of remainder,n mod m is the integer r such that n = qm + r for q an integer, <strong>and</strong> |r| < |m|.Therefore, the result of n mod m must be between 0 <strong>and</strong> m − 1 when n <strong>and</strong> m are

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

Saved successfully!

Ooh no, something went wrong!