From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill Newsgroups: gmane.emacs.devel Subject: Re: Tree-sitter indentation for js-mode & cc-mode Date: Fri, 28 Oct 2022 10:59:22 +0200 Message-ID: <87pmecgw7p.fsf@thornhill.no> References: <9AF8BFDC-C9A2-4AE5-A8D2-E6AA05DA3C91@gmail.com> <87k04lljh6.fsf@thornhill.no> <541CF451-6FAC-4531-A8AF-8C86FBB9D40B@thornhill.no> <425E0075-3F44-4832-BA2E-61E7D0A26FF4@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38931"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel , Stefan Monnier To: Yuan Fu Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Oct 28 11:01:19 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ooLEs-0005h6-6l for ged-emacs-devel@m.gmane-mx.org; Fri, 28 Oct 2022 11:01:19 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ooLDM-0006Qr-PI; Fri, 28 Oct 2022 04:59:46 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ooLDJ-0006NA-AD for emacs-devel@gnu.org; Fri, 28 Oct 2022 04:59:41 -0400 Original-Received: from out2.migadu.com ([188.165.223.204]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ooLD6-0003qj-PJ for emacs-devel@gnu.org; Fri, 28 Oct 2022 04:59:41 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1666947565; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=A8fjG/Nj0baoRhLGKbPHDxZ4CDF0t86S16i+rgajjos=; b=d0Uif4vp7lCQTaE5CX8ehl84DgoHbKJRoCPqQ4Ly3qKHEUoQq5qfpCmVjz6wc2D+WDR8IA YioNS4dJDs8N1YIEUkIHxviW2XJQxqGYOVMBg1Mee1cSwTRbaC++Ruul2WL7ypewsLsoer 4as9ISIg3X3+/jlfUJyQCmF3gcDdsX4+US73zgklVGZOU6gpGAJjD1xNfSohRUdukxrxCA qZOsA4IHU+I/EkgyMwmQnMIfFMbnyzAuKfOD/VuLIc6IpMZuT2E/Ab+ZU80+taBL1yNyV8 JN+fvCEbDCnmulg09VNLNIZIu2vzlwsVHl2eZeligsFR0NE3mHUF2dh7AJtWbw== In-Reply-To: <425E0075-3F44-4832-BA2E-61E7D0A26FF4@gmail.com> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=188.165.223.204; envelope-from=theo@thornhill.no; helo=out2.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: "Emacs-devel" Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:298666 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Yuan Fu writes: >>=20 >>>> looking up way to much the root of the tree, but you know the internals >>>> here better than me. Is this something we can optimize away? See the >>>> attached report at the bottom. >>>=20 >>> This is very strange, I need to look into it. >>>=20 >>=20 >> I'm happy to provide more info and profiling, as well as testing if >> need be! > > I just tried running treesit-buffer-root-node and treesit-node-at > 10000 times in the end of buffer and they are pretty fast, so I don=E2=80= =99t > know why the benchmark says 99% time is spent in > treesit-buffer-root-node. Could you share the benchmark code and test > file? Thanks! Absolutely. I ran the test again - see test file and new report in attachments. You need to `M-x eval-buffer` in `treesit.el` to avoid the compiled functions to get better profile report, then in the testfile: M-x profiler-start C-x h ;; (mark-whole-buffer) C-i ;; (indent-for-tab-command) ;; --- waaaaait M-x profiler-stop M-x profiler-report There's no test code for this, just running the commands sequentially and get the report :-) Are we parsing the whole file over and over in treesit-buffer-root-node? Do we for some reason not hit the early return? Code is taken from [0] and duplicated and messed up indentation. Theo [0] https://raw.githubusercontent.com/TheAlgorithms/JavaScript/master/Searc= h/BinarySearch.js --=-=-= Content-Type: text/javascript Content-Disposition: attachment; filename=foo.js function binarySearchRecursive (arr, x, low = 0, high = arr.length - 1) { const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index return mid } if (x < arr[mid]) { // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item return -1 } } --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=cpu.txt 177699 99% - command-execute 177652 99% - funcall-interactively 177652 99% - indent-for-tab-command 177652 99% - indent-region 177628 99% - indent-region-line-by-line 177516 99% - indent-according-to-mode 177504 99% - treesit-indent 177484 99% - let* 174145 97% - cond 174125 97% - treesit-node-at 174109 97% - let* 174101 97% - if 173969 97% - treesit-buffer-root-node 173961 97% - let* 173957 97% if 132 0% - progn 120 0% - while 120 0% setq 8 0% if 4 0% - eq 4 0% length 3179 1% - let* 2488 1% - funcall 2476 1% - treesit-simple-indent 2468 1% - if 2456 1% - let* 2372 1% - let* 2356 1% - while 2328 1% - and 2320 1% - progn 2260 1% - if 1940 1% - treesit--simple-indent-eval 1904 1% - cond 1872 1% - apply 1260 0% - treesit--simple-indent-eval 1248 0% - cond 1220 0% - apply 1080 0% - # 772 0% - ` 572 0% - # 536 0% - backquote-process 392 0% - backquote-process 148 0% - backquote-process 68 0% backquote-process 8 0% backquote-listify 12 0% backquote-listify 176 0% - list 32 0% - backquote-list* 12 0% cons 40 0% - treesit--simple-indent-eval 24 0% - cond 4 0% and 36 0% - mapcar 28 0% - treesit--simple-indent-eval 20 0% cond 12 0% - and 4 0% not 224 0% - mapcar 160 0% - treesit--simple-indent-eval 144 0% - cond 4 0% and 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% string-match-p 4 0% - # 4 0% string-match-p 4 0% - # 4 0% string-match-p 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% # 4 0% - # 4 0% - string-match-p 4 0% or 4 0% # 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% # 4 0% - # 4 0% - string-match-p 4 0% or 4 0% # 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% string-match-p 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% - # 4 0% - string-match-p 4 0% or 4 0% and 292 0% - progn 288 0% - progn 276 0% - setq 252 0% - let 232 0% - treesit--simple-indent-eval 224 0% - cond 212 0% - apply 140 0% - # 96 0% - save-excursion 4 0% goto-char 40 0% - mapcar 24 0% - treesit--simple-indent-eval 24 0% cond 16 0% - treesit--simple-indent-eval 8 0% cond 4 0% and 8 0% cons 32 0% setq 4 0% setq 4 0% treesit-node-language 643 0% - progn 631 0% - let* 623 0% - let 623 0% - if 623 0% - let 483 0% - if 67 0% - indent-line-to 39 0% - # 39 0% - filter-buffer-substring 39 0% - buffer-substring--filter 39 0% - # 39 0% apply 132 0% - + 120 0% save-excursion 32 0% cond 104 0% - treesit-parent-while 80 0% - let 76 0% - while 48 0% - progn 44 0% setq 16 0% - and 8 0% - funcall 8 0% - # 4 0% eq 20 0% save-excursion 47 0% - byte-code 47 0% - read-extended-command 47 0% - read-extended-command-1 47 0% - completing-read-default 47 0% - apply 47 0% - vertico--advice 31 0% - # 20 0% - vertico--exhibit 20 0% - vertico--update 20 0% - vertico--recompute 16 0% - vertico--all-completions 16 0% - completion-all-completions 16 0% - completion--nth-completion 16 0% - completion--some 16 0% - # 16 0% - completion-basic-all-completions 16 0% - completion-pcm--all-completions 16 0% - # 16 0% - complete-with-action 8 0% - all-completions 8 0% - # 8 0% # 4 0% vertico-sort-history-length-alpha 7 0% - frame-windows-min-size 7 0% - window-min-size 7 0% - window--min-size-1 7 0% window--min-size-1 1298 0% + ... 35 0% + # 28 0% + timer-event-handler 23 0% + redisplay_internal (C function) --=-=-=--