📜  使用二分查找求一个数的平方根的Java程序

📅  最后修改于: 2022-05-13 01:54:33.236000             🧑  作者: Mango

使用二分查找求一个数的平方根的Java程序

给定一个非负数,使用二进制搜索方法找到一个数的平方根。

例子 :

Input: x = 16
Output: 4
Explanation:  The square root of 16 is 4.

Input: x = 5
Output: 2
Explanation:  The square root of 5 lies in between
2 and 3 so floor of the square root is 2.

天真的方法:

  • 检查每个元素的平方直到 n 并存储答案直到平方小于或等于 n
Java
// Java program to Find the square root of given numbers
// by brute force technique
  
import java.io.*;
  
class GFG {
    static int cuberoot(int n)
    {
        int ans = 0;
  
        for (int i = 1; i <= n; ++i) {
            
            // checking every number cube
            if (i * i <= n) {
                ans = i;
            }
        }
        return ans;
    }
    public static void main(String[] args)
    {
        // Number
        int number = 16;
        
        // Checking number
        int cuberoot = cuberoot(number);
        System.out.println(cuberoot);
    }
}


Java
// Java program to Find the square root of given numbers
// using Binary search
  
// Importing libraries
import java.io.*;
import java.util.*;
class GFG {
    
    // Function to find cuberoot
    static int squareeroot(int number)
    {
        // Lower bound
        int left = 1;
        
        // Upper bound
        int right = number;
  
        int ans = 0;
        while (left <= right) {
            
            // Finding the mid value
  
            int mid = left + (right - left) / 2;
            
            // Checking the mid value
            if (mid * mid == number) {
                return mid;
            }
  
            // Shift the lower bound
            if (mid * mid < number) {
                left = mid + 1;
                ans = mid;
            }
            
            // Shift the upper bound
            else {
                right = mid - 1;
            }
        }
        
        // Return the ans
        return ans;
    }
    public static void main(String[] args)
    {
        int number = 15;
        System.out.println(squareroot(number));
    }
}


输出
4

空间复杂度: O(1)



时间复杂度: O(n)

高效方法(二分搜索):二分搜索使用分而治之的方法,使得复杂度为 O(logn)。

算法:

  • 初始化 left=0 和 right =n
  • 计算mid=left+(right-left)/2
  • 如果mid*mid 等于该数字,则返回mid。
  • 如果 mid*mid 小于将 mid 存储在 an 中的数字,因为这可能是答案并增加 left=mid+1 并且现在检查右半部分。
  • 如果 mid*mid 大于数字并减小 right=mid-1,因为预期值较小,因此我们现在将查看左半部分或将扫描较小的值。
  • 返回答案

执行:

Java

// Java program to Find the square root of given numbers
// using Binary search
  
// Importing libraries
import java.io.*;
import java.util.*;
class GFG {
    
    // Function to find cuberoot
    static int squareeroot(int number)
    {
        // Lower bound
        int left = 1;
        
        // Upper bound
        int right = number;
  
        int ans = 0;
        while (left <= right) {
            
            // Finding the mid value
  
            int mid = left + (right - left) / 2;
            
            // Checking the mid value
            if (mid * mid == number) {
                return mid;
            }
  
            // Shift the lower bound
            if (mid * mid < number) {
                left = mid + 1;
                ans = mid;
            }
            
            // Shift the upper bound
            else {
                right = mid - 1;
            }
        }
        
        // Return the ans
        return ans;
    }
    public static void main(String[] args)
    {
        int number = 15;
        System.out.println(squareroot(number));
    }
}
输出
3

时间复杂度: O(logn)