function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepak Kumar Singh 15Deepak Kumar Singh 15 

How to Implement Binary Search algorithm using Apex & Collections?

Best Answer chosen by Deepak Kumar Singh 15
Suraj Tripathi 47Suraj Tripathi 47
Hi Deepak,

Try this code.
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());
        }
        
    }
}

Example :
Anonymous Window:-
 
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:-

User-added image

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

AbhinavAbhinav (Salesforce Developers) 
Hi Deepak,

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!
Suraj Tripathi 47Suraj Tripathi 47
Hi Deepak,

Try this code.
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());
        }
        
    }
}

Example :
Anonymous Window:-
 
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:-

User-added image

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

This was selected as the best answer