Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
12
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { static int binarySearch(String[] book, String x) { int left = 0, right = book.Length - 1; while (left <= right) { int mid = left + (right - left) / 2; int res = x.CompareTo(book[mid]); if (res == 0) return mid; if (res > 0) left = mid + 1; else right = mid - 1; } return -1;} public static void Main(String []args) { String[] book = {"Archaeology", "Art", "Biology", "Chemistry", "Computing", "English", "French", "Geography", "History", "Maths", "Psychology"}; String x = Console.ReadLine(); int result = binarySearch(book, x); if (result == -1) Console.WriteLine("Element not present"); else Console.WriteLine("Element found at " + "index " + result); }}}
Art
Show compiler warnings
[
-
]
Show input
Compilation time: 0,23 sec, absolute running time: 0,11 sec, cpu time: 0,11 sec, average memory usage: 14 Mb, average nr of threads: 3, absolute service time: 0,36 sec
edit mode
|
history
|
discussion
Element found at index 1