Change the implementation of the following method so that it returns the index of element found in the search if the sea

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Change the implementation of the following method so that it returns the index of element found in the search if the sea

Post by answerhappygod »

Change The Implementation Of The Following Method So That It Returns The Index Of Element Found In The Search If The Sea 1
Change The Implementation Of The Following Method So That It Returns The Index Of Element Found In The Search If The Sea 1 (118.71 KiB) Viewed 27 times
Change the implementation of the following method so that it returns the index of element found in the search if the search is successful and returns -belongsAt-1 if not successful where belongsAt is the index where the element has to be inserted in the array so that the array remains sorted. private static < T extends Comparable < ? super T>> boolean binarySearch(T[] anArray, int first, int last, T desiredltem) boolean found; int mid = first + (last − first )/2; if (first > last) found = false; else if (desiredltem.equals(anArray[mid])) found = true; else if (desiredltem.compareTo(anArray[mid ]<0 ) found = binarySearch(anArray, first, mid - 1, desiredltem); else found = binarySearch(anArray, mid + 1, last, desiredltem); return found; \} // end binarySearch public static < T extends ComparableT>> boolean inArray (T anEntry ) \{ return binarySearch(anArray, 0, anArray.length - 1, anEntry); \}// end inArray
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply