Section2Problems 1 - CSE143 Section Below is Lhe next...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE143 Section Below is Lhe next search a binary assignment: method from the #2 Problems Arrays class that we wil-I 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 wj-l-I 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+1-i (midVal else if high:mid-I; 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 tl-03 caII: 1. What values the fol-lowing Arrays.binarySearch and what value wou.ld be low, (l-ist, 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

This note was uploaded on 11/03/2009 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Ask a homework question - tutors are online