ÿØÿà JFIF ÿÛ „ ( %!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
ÿØÿà JFIF ÿÛ „
(
%!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ
ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ
ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ
? ¼QxJQaÍuò¸Zö
Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
????????????????????????????????????
????????????????????????????????????
ÿØÿà JFIF ÿÛ „ ( %!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
ÿØÿà JFIF ÿÛ „
(
%!1!%)+//.383,7(-.+
-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ ¥2" ÿÄ
ÿÄ J ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ
ÿÄ * !1AQa‘"2q3±ð#b¡ÿÚ
? ¼QxJQaÍuò¸Zö
Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁTÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßwpŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
?>
.........................................
.............................................................................
????????????????????????????????????
????????????????????????????????????
U:RDoc::TopLevel[ i I"bsearch.rdoc:ETcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[8S:RDoc::Markup::Heading:
leveli: textI"Binary Searching;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"AA few Ruby methods support binary searching in a collection:;T@
o:RDoc::Markup::List:
@type: NOTE:@items[o:RDoc::Markup::ListItem:@label[I"Array#bsearch;T;[o;
;[I"5Returns an element selected via a binary search ;TI"$as determined by a given block.;To;;[I"Array#bsearch_index;T;[o;
;[I"BReturns the index of an element selected via a binary search ;TI"$as determined by a given block.;To;;[I"Range#bsearch;T;[o;
;[I"5Returns an element selected via a binary search ;TI"$as determined by a given block.;T@
o;
;[I"FEach of these methods returns an enumerator if no block is given.;T@
o;
;[
I"\Given a block, each of these methods returns an element (or element index) from +self+ ;TI"'as determined by a binary search. ;TI"7The search finds an element of +self+ which meets ;TI"^the given condition in O(log n) operations, where +n+ is the count of elements. ;TI"6+self+ should be sorted, but this is not checked.;T@
o;
;[I" There are two search modes:;T@
o;;;;[o;;[I"Find-minimum mode;T;[o;
;[I":method +bsearch+ returns the first element for which ;TI"the block returns +true+; ;TI"-the block must return +true+ or +false+.;To;;[I"Find-any mode;T;[o;
;[I"6method +bsearch+ some element, if any, for which ;TI"the block returns zero. ;TI"+the block must return a numeric value.;T@
o;
;[I"QThe block should not mix the modes by sometimes returning +true+ or +false+ ;TI"Hand other times returning a numeric value, but this is not checked.;T@
o;
;[I"Find-Minimum Mode;T@
o;
;[I"DIn find-minimum mode, the block must return +true+ or +false+. ;TI":The further requirement (though not checked) is that ;TI"0there are no indexes +i+ and +j+ such that:;T@
o;;:BULLET;[o;;0;[o;
;[I"&0 <= i < j <= self.size.;To;;0;[o;
;[I"TThe block returns +true+ for self[i] and +false+ for self[j].;T@
o;
;[I"KLess formally: the block is such that all +false+-evaluating elements ;TI",precede all +true+-evaluating elements.;T@
o;
;[I"FIn find-minimum mode, method +bsearch+ returns the first element ;TI"(for which the block returns +true+.;T@
o;
;[I"Examples:;T@
o:RDoc::Markup::Verbatim;[I"a = [0, 4, 7, 10, 12]
;TI"$a.bsearch {|x| x >= 4 } # => 4
;TI"$a.bsearch {|x| x >= 6 } # => 7
;TI"%a.bsearch {|x| x >= -1 } # => 0
;TI"(a.bsearch {|x| x >= 100 } # => nil
;TI"
;TI"r = (0...a.size)
;TI"&r.bsearch {|i| a[i] >= 4 } #=> 1
;TI"&r.bsearch {|i| a[i] >= 6 } #=> 2
;TI"&r.bsearch {|i| a[i] >= 8 } #=> 3
;TI"*r.bsearch {|i| a[i] >= 100 } #=> nil
;TI"!r = (0.0...Float::INFINITY)
;TI"/r.bsearch {|x| Math.log(x) >= 0 } #=> 1.0
;T:@format0o;
;[I"2These blocks make sense in find-minimum mode:;T@
o;;[
I"a = [0, 4, 7, 10, 12]
;TI">a.map {|x| x >= 4 } # => [false, true, true, true, true]
;TI"?a.map {|x| x >= 6 } # => [false, false, true, true, true]
;TI">a.map {|x| x >= -1 } # => [true, true, true, true, true]
;TI"Da.map {|x| x >= 100 } # => [false, false, false, false, false]
;T;0o;
;[I"This would not make sense:;T@
o;;[I"Aa.map {|x| x == 7 } # => [false, false, true, false, false]
;T;0o;
;[I"Find-Any Mode;T@
o;
;[I">In find-any mode, the block must return a numeric value. ;TI":The further requirement (though not checked) is that ;TI"0there are no indexes +i+ and +j+ such that:;T@
o;;;;[ o;;0;[o;
;[I"&0 <= i < j <= self.size.;To;;0;[o;
;[I"=The block returns a negative value for self[i] ;TI"/and a positive value for self[j].;To;;0;[o;
;[I"WThe block returns a negative value for self[i] and zero self[j].;To;;0;[o;
;[I"[The block returns zero for self[i] and a positive value for self[j].;T@
o;
;[I"+Less formally: the block is such that:;T@
o;;;;[o;;0;[o;
;[I"KAll positive-evaluating elements precede all zero-evaluating elements.;To;;0;[o;
;[I"OAll positive-evaluating elements precede all negative-evaluating elements.;To;;0;[o;
;[I"KAll zero-evaluating elements precede all negative-evaluating elements.;T@
o;
;[I"=In find-any mode, method +bsearch+ returns some element ;TI"Lfor which the block returns zero, or +nil+ if no such element is found.;T@
o;
;[I"Examples:;T@
o;;[I"a = [0, 4, 7, 10, 12]
;TI"1a.bsearch {|element| 7 <=> element } # => 7
;TI"4a.bsearch {|element| -1 <=> element } # => nil
;TI"3a.bsearch {|element| 5 <=> element } # => nil
;TI"4a.bsearch {|element| 15 <=> element } # => nil
;TI"
;TI"!a = [0, 100, 100, 100, 200]
;TI"r = (0..4)
;TI"/r.bsearch {|i| 100 - a[i] } #=> 1, 2 or 3
;TI")r.bsearch {|i| 300 - a[i] } #=> nil
;TI")r.bsearch {|i| 50 - a[i] } #=> nil
;T;0o;
;[I".These blocks make sense in find-any mode:;T@
o;;[
I"a = [0, 4, 7, 10, 12]
;TI"=a.map {|element| 7 <=> element } # => [1, 1, 0, -1, -1]
;TI"Aa.map {|element| -1 <=> element } # => [-1, -1, -1, -1, -1]
;TI">a.map {|element| 5 <=> element } # => [1, 1, -1, -1, -1]
;TI" element } # => [1, 1, 1, 1, 1]
;T;0o;
;[I"This would not make sense:;T@
o;;[I" 7 } # => [-1, -1, 0, 1, 1];T;0:
@file@:0@omit_headings_from_table_of_contents_below0