function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
Public class BinaryExample{ public static void binarySearch(list<integer> integerList, integer key){ try{ integer first=0; integer last=integerList.size()-1; integer mid = (first + last)/2; while( first <= last ){ if ( integerList[mid] < key ){ first = mid + 1; }else if (integerList[mid] == key){ System.debug('Element is found at: ' + mid); break; }else{ last = mid - 1; } mid = (first + last)/2; } if ( first > last ){ System.debug('Element is not found!'); } }catch(exception e){ system.debug('Error:'+e.getMessage()+'At Line:'+e.getLineNumber()); } } }
list<integer> numval = new list<integer>{10,20,30,40,50,70,80,90,100,110,120,130,150}; integer key = 120; BinaryExample.binarySearch(numval,key);
output:-
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others tooThanks and Regards,Suraj Tripathi
Try this code.
Example :
Anonymous Window:-
output:-
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too
Thanks and Regards,
Suraj Tripathi
All Answers
Check this blog it has detailed implementation for you use case:
https://www.sfdcamplified.com/2020/12/logic-building-binary-search.html
If it helps mark it as best answer.
Thanks!
Try this code.
Example :
Anonymous Window:-
output:-
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too
Thanks and Regards,
Suraj Tripathi