Unformatted text preview: CSE143 Section Below is Lhe next search a binary assignment: method from the #2 Problems Arrays class that we wilI be using for (exclusive) (inclusive) to tolndex : elements in indexes fromlndex / / pre (nondecreasing) order in sorted are // within the range returns the index of the given key in the list / / post: returns (inclusive) through tofndex(exclusive); fromlndex //  1) otherwise. point is The insertion ((insertion point) // into the at which the key would be inserted as the point defined // than element in the range greater array: the index of the first // if all elements in the range are less than the the key, or tolndex // value guarantees that. the return Note that this key. specified // If the Iist is found. if and only if the key wjlI be ):0 // is multiple copies of the key in the given range, there contains // guarantee which index is returned. no // tolndex, fromlndex"r int int Iist, public int binarySearch(intlJ static int k"y) { int low : fromlndex; 1; int high = tolndex whi,le (low <= high) { int mid = (Iow + high) / int midVa1 = Iist 1mid1; (midVal low:mid+1i (midVal else if high:midI; else mid; return if
t 2; // // key key found not found. as follows: return ) Suppose that int This would (low + 1); that we have {2,8, the an array 13, called list initialized 42,51' L03}; Iist: 22,25,25,38, array: construct following list ++  ++> ++ t7l t 6t tsl t4l t,2t t3t tll +++++++++++ 38 142 13 122  8l I 125 125  2  ++++++++++r+ t0l
would low, high and mid take on for
0, 10' 103) t8l tel
I s1 tl03 caII: 1. What values the following Arrays.binarySearch and what value wou.ld be low, (list, returned? high and mid 2' 9, take 30) return value represent? on for the following call: What values would Arrays.binarySearch(Iist, and what vaLue would be returned? What does this ...
View
Full Document
 Spring '08
 SR
 Sort, Subroutine, The Return, insertion point, int int Iist

Click to edit the document details