【问题标题】:Fixed Point (Value equal to index) in a given array - 1 indexed using binary search给定数组中的定点(值等于索引) - 1 使用二进制搜索索引
【发布时间】:2020-11-28 09:03:46
【问题描述】:

是否可以修改此代码,使索引值从 1 开始,而不是 0? 因此,如果数组是 0 1 3 4,程序将返回 3。

class Main 
    { 
        static int binarySearch(int arr[], int low, int high) 
        { 
            if(high >= low) 
            {    
                /* low + (high - low)/2; */
                int mid = (low + high)/2;   
                if(mid == arr[mid]) 
                    return mid; 
                if(mid > arr[mid]) 
                    return binarySearch(arr, (mid + 1), high); 
                else
                    return binarySearch(arr, low, (mid -1)); 
            } 
            
            /* Return -1 if there is  
               no Fixed Point */
            return -1; 
        } 
            
        //main function 
        public static void main(String args[]) 
        { 
            int arr[] = {-10, -1, 0, 3 , 10, 11, 30, 50, 100}; 
            int n = arr.length; 
            System.out.println("Fixed Point is " 
                       + binarySearch(arr,0, n-1));         
        }  
    } 

【问题讨论】:

    标签: java arrays algorithm search binary-search


    【解决方案1】:

    只需在调用binarySearch时将0更改为1,将n-1更改为n并在arr的0索引处添加一个随机数,即执行以下操作:

    int arr[] = {99999999/*note this random large number*/, -10, -1, 0, 4/*note this 4*/ , 10, 11, 30, 50, 100}; 
    

    binarySearch(arr, 1, n);
    

    带证明的完整示例:

    public class SOTest 
        { 
            static int binarySearch(int arr[], int low, int high) 
            { 
                if(high >= low) 
                {    
                    /* low + (high - low)/2; */
                    int mid = (low + high)/2;   
                    if(mid == arr[mid]) 
                        return mid; 
                    if(mid > arr[mid]) 
                        return binarySearch(arr, (mid + 1), high); 
                    else
                        return binarySearch(arr, low, (mid -1)); 
                } 
                
                /* Return -1 if there is  
                   no Fixed Point */
                return -1; 
            } 
                
            //main function 
            public static void main(String args[]) 
            { 
                int arr[] = {99999999/*some very positive value*/, -10, -1, 0, 4/*note this 4*/, 10, 11, 30, 50, 100}; 
                int n = arr.length; 
                System.out.println("Fixed Point is " 
                           + binarySearch(arr, 1, n));         
            }  
        } 
    

    输出:

    Fixed Point is 4
    

    【讨论】:

      【解决方案2】:

      您可以将二分搜索替换为 (mid+1)。

      class Main 
          { 
              static int binarySearch(int arr[], int low, int high) 
              { 
                  if(high >= low) 
                  {    
                      /* low + (high - low)/2; */
                      int mid = low+(high-low)/2;   
                      if((mid+1) == arr[mid]) 
                          return mid+1; 
                      if((mid+1) > arr[mid]) 
                          return binarySearch(arr, (mid + 1), high); 
                      else
                          return binarySearch(arr, low, (mid -1)); 
                  } 
                  
                  /* Return -1 if there is  
                     no Fixed Point */
                  return -1; 
              } 
                  
              //main function 
              public static void main(String args[]) 
              { 
                  int arr[] = {0,1,3,4}; 
                  int n = arr.length; 
                  System.out.println("Fixed Point is " 
                             + binarySearch(arr,0, n-1));         
              }  
          } 
      

      输入:

      arr = {0,1,3,4}

      输出:

      固定点是 3

      【讨论】:

      • 它不适用于数组 {1,2,3,4},在这种情况下,它将返回 2,而不是 1。
      • @dryre 是二分查找,会找到可满足的条件...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多