We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicint?BinarySearch(int[]A,intT){// Given an array A of n elements with values or records A0 ... An−1, sorted such that A0 ≤ ... ≤ An−1, and target value T, the following subroutine uses binary search to find the index of T in A.[6]// 1. Set L to 0 and R to n − 1.// 2. If L > R, the search terminates as unsuccessful.// 3. Set m(the position of the middle element) to the floor of (L + R) / 2.// 4. If Am < T, set L to m + 1 and go to step 2.// 5. If Am > T, set R to m – 1 and go to step 2.// 6. Now Am = T, the search is done; return m.Array.Sort(A);intM;intL=0;intR=A.Length-1;while(L<=R){M=(L+R)/2;if(A[M]<T)L=M+1;elseif(A[M]>T)R=M-1;elsereturnM;}returnnull;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Intro to Tutorial Challenges
You are viewing a single comment's thread. Return to all comments →