⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.93
Server IP:
65.108.141.171
Server:
Linux server.heloix.com 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
Server Software:
Apache
PHP Version:
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
ri
/
2.7.0
/
system
/
Array
/
View File Name :
bsearch-i.ri
U:RDoc::AnyMethod[iI"bsearch:ETI"Array#bsearch;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"GBy using binary search, finds a value from this array which meets ;TI"Fthe given condition in O(log n) where n is the size of the array.;To:RDoc::Markup::BlankLine o; ; [I"CYou can use this method in two modes: a find-minimum mode and ;TI"Ia find-any mode. In either case, the elements of the array must be ;TI"4monotone (or sorted) with respect to the block.;T@o; ; [I"IIn find-minimum mode (this is a good choice for typical use cases), ;TI"Nthe block must always return true or false, and there must be an index i ;TI""(0 <= i <= ary.size) so that:;T@o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o; ; [I"Fthe block returns false for any element whose index is less than ;TI"i, and;To;;0; [o; ; [I"Cthe block returns true for any element whose index is greater ;TI"than or equal to i.;T@o; ; [I"GThis method returns the i-th element. If i is equal to ary.size, ;TI"it returns nil.;T@o:RDoc::Markup::Verbatim; [ I"ary = [0, 4, 7, 10, 12] ;TI"'ary.bsearch {|x| x >= 4 } #=> 4 ;TI"'ary.bsearch {|x| x >= 6 } #=> 7 ;TI"'ary.bsearch {|x| x >= -1 } #=> 0 ;TI")ary.bsearch {|x| x >= 100 } #=> nil ;T:@format0o; ; [I"GIn find-any mode (this behaves like libc's bsearch(3)), the block ;TI"Hmust always return a number, and there must be two indices i and j ;TI"'(0 <= i <= j <= ary.size) so that:;T@o;; ;;[o;;0; [o; ; [I"Bthe block returns a positive number for ary[k] if 0 <= k < i,;To;;0; [o; ; [I"9the block returns zero for ary[k] if i <= k < j, and;To;;0; [o; ; [I"7the block returns a negative number for ary[k] if ;TI"j <= k < ary.size.;T@o; ; [I"GUnder this condition, this method returns any element whose index ;TI"Eis within i...j. If i is equal to j (i.e., there is no element ;TI"8that satisfies the block), this method returns nil.;T@o;; [ I"ary = [0, 4, 7, 10, 12] ;TI"*# try to find v such that 4 <= v < 8 ;TI"-ary.bsearch {|x| 1 - x / 4 } #=> 4 or 7 ;TI"+# try to find v such that 8 <= v < 10 ;TI"*ary.bsearch {|x| 4 - x / 2 } #=> nil ;T;0o; ; [I"EYou must not mix the two modes at a time; the block must always ;TI"Areturn either true/false, or always return a number. It is ;TI"Cundefined which value is actually picked up at each iteration.;T: @fileI"array.c;T:0@omit_headings_from_table_of_contents_below0I"'ary.bsearch {|x| block } -> elem ;T0[ I"();T@[FI" Array;TcRDoc::NormalClass00