Share my C# and javascript binary search code Recursive and non Recursive


  • 0
    Y

    C#: non Recursive

    public class Solution {

    public int SearchInsert(int[] nums, int target) {
        
        if(nums.Length==0)
        {
            return 0;
        }
        
        int low = 0;
        int high = nums.Length - 1;
        int middle;
        
        while(low < high)
        {
            middle=(low+high)/2;
            if(nums[middle]==target)
            {
                return middle;
            }
            else if(nums[middle]>target)
            {
                high=middle-1;
            }
            else
            {
                low = middle + 1;
            }
        }
        
        if(nums[low] < target)
        {
            return low + 1;
        }
        else
        {
            return low;
        }
        
      }
    

    }

    C# Recursive :

    public int SearchInsertRecurrence(int[] nums, int low, int high ,int target)
    {
    if(low >= high)
    {
    if(nums[low] < target)
    {
    return low + 1;
    }
    else
    {
    return low;
    }
    }

            int middle=(low+high)/2;
    	 
            if(nums[middle]==target)
            {
                return middle;
            }
            else if(nums[middle] > target)
            {
                return SearchInsertRecurrence(nums, low, middle-1,target);
            }
            else
            {
                return SearchInsertRecurrence(nums, middle + 1, high,target);
            }        
            
    }
    
    Javascript:
    
    
    /**
       * @param {number[]} nums
    * @param {number} target
    * @return {number}
    */
    

    var searchInsert = function(nums, target) {

        if(nums.length===0)
        {
            return 0;
        }
        
        var low = 0;
        var high = nums.length - 1;
        var middle;
        
        while(low < high)
        {
            middle = Math.floor((low + high)/2);
            if(nums[middle] === target)
            {
                return middle;
            }
            else if(nums[middle] > target)
            {
                high = middle - 1;
            }
            else
            {
                low = middle + 1;
            }
        }
        
        if(nums[low] < target)
        {
            return low + 1;
        }
        else
        {
            return low;
        }
    

    };

    //Recursively

    var SearchInsertRecurrence = function(nums, low, high ,target)
    {
    if(low >= high)
    {
    if(nums[low] < target)
    {
    return low + 1;
    }
    else
    {
    return low;
    }
    }

            var middle=Math.floor((low+high)/2);
    	 
            if(nums[middle]===target)
            {
                return middle;
            }
            else if(nums[middle] > target)
            {
                return SearchInsertRecurrence(nums, low, middle-1,target);
            }
            else
            {
                return SearchInsertRecurrence(nums, middle + 1, high,target);
            }        
            
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.