How to print math that a computer performs for binary search

I am working on a program that asks the user to enter a value between 3000-3100 and perform a binary search and show how many comparisons he made. In general, the search part of the program works fine; however, my professor wants me to print a program that does math. For example, I need a program to show a computer that performs mathematical math search, and show the comparisons that are required for the program to find the entered number.

I have comparisonCountthat I increase it when I make a comparison, but the results are not what I think they should be. For example, my professor said that if you enter 3067, there should be 7 comparisons, but currently the program says 4 and the counter says 3.

Can you help me find the cause of the discrepancy?

Here is the code:

package binary.search;
import java.util.Scanner;

public class BinarySearch
{
    public static void binarySearch(int[] array, int lowerbound, int upperbound,         int key)
    {
        int position;
        int comparisonCount = 1;   // counting the number of comparisons
        // To start, find the subscript of the middle position.
        position = (lowerbound + upperbound) / 2;
        //System.out.println();
        while ((array[position] != key) && (lowerbound <= upperbound))
        {
            comparisonCount++;
            if (array[position] > key)          // If the number is > key, ..
            {
                upperbound = position - 1;     // decrease position by one.
            }
            else
            {
                lowerbound = position + 1;    // Else, increase position by one.
            }
            position = (lowerbound + upperbound) / 2;
        }
        if (lowerbound <= upperbound)
        {
            System.out.println("The number " + key + " was found in array.");
            System.out.println("The binary search found the number after " + comparisonCount + " comparisons.");
        }
        else
            System.out.println("That number is not in this array. The binary search completed "
                + comparisonCount +  " comparisons.");


    }

    public static void main(String[] args)
    {
        // Set up variables

        int arrLength = 100;
        Scanner inp = new Scanner(System.in);
        int[] num = new int[arrLength]; //Create array
        int repeat = 1;            //Boolean for repeat loop

        char yesNo;
        int upperLim = 3100;
        int lowerLim = 3000;

        //Populate array
        while (repeat == 1)
        {
            int value = 0;
            int valid = 0;

            for (int i = 0; i < num.length; i++)
            {
                num[i] = i + lowerLim;
            }

            //Get integer from user
            do
            {
                System.out.print("Please enter a number between " + lowerLim + " and " + upperLim + ": ");
                value = inp.nextInt();

                if (value < lowerLim || value > upperLim)
                {
                    System.out.print("That wasn't a valid number. Please try again. \n");
                }
            }
            while (value < lowerLim || value > upperLim);

            //Run binary search
            binarySearch(num, 0, arrLength - 1, value);

            do
            {
                valid = 0;
                System.out.print("Would you like to rerun the program? Y for yes, N for no.\n");
                yesNo = (inp.next()).charAt(0);
                if (yesNo == 'Y')
                {
                    repeat = 1;
                    valid = 1;
                }
                else if (yesNo == 'N')
                {
                    repeat = 0;
                    valid = 1;
                }
                else
                    System.out.print("Not a valid response. \n");
            }
            while (valid != 1);
        }
    }
}
+4
source share
1 answer

Seven iterations is the maximum required to find a number in a set of one hundred. This is because it is somewhere between six and seven, and you need to use the higher of these two values ​​to make sure it is detected.log2100

- , . , , ( , //<<here //<<):

while ((array[position] != key) && (lowerbound <= upperbound)) {
    System.out.print(String.format(                        //<<here
        "Step %d, lo=%2d, hi=%2d, mid=%2d, [mid]=%4d, ", 
        comparisonCount, lowerbound, upperbound,
        position, array[position]));                       //<<
    comparisonCount++;
    if (array[position] > key) {
        upperbound = position - 1;
        System.out.println(String.format(                  //<<here
            "too high, hi:=%2d", upperbound));             //<<
    } else {
        lowerbound = position + 1;
        System.out.println(String.format(                  //<<here
            "too low,  lo:=%2d", lowerbound));             //<<
    }
    position = (lowerbound + upperbound) / 2;
}
if (lowerbound <= upperbound) {
    System.out.println(String.format(                      //<<here
        "Step %d, lo=%2d, hi=%2d, mid=%2d, [mid]=%4d, found!", 
        comparisonCount, lowerbound, upperbound,
        position, array[position]));                       //<<
    System.out.println("The number " + key +
        " was found in array.");
    System.out.println("The binary search found the number after "
        + comparisonCount + " comparisons.");
}

, 3049, :

Step 1: lo= 0, hi=99, mid=49, [mid]=3049, found!

3067 :

Step 1: lo= 0, hi=99, mid=49, [mid]=3049, too low,  lo:=50
Step 2: lo=50, hi=99, mid=74, [mid]=3074, too high, hi:=73
Step 3: lo=50, hi=73, mid=61, [mid]=3061, too low,  lo:=62
Step 4: lo=62, hi=73, mid=67, [mid]=3067, found!

, . , 3099:

Step 1, lo= 0, hi=99, mid=49, [mid]=3049, too low,  lo:=50
Step 2, lo=50, hi=99, mid=74, [mid]=3074, too low,  lo:=75
Step 3, lo=75, hi=99, mid=87, [mid]=3087, too low,  lo:=88
Step 4, lo=88, hi=99, mid=93, [mid]=3093, too low,  lo:=94
Step 5, lo=94, hi=99, mid=96, [mid]=3096, too low,  lo:=97
Step 6, lo=97, hi=99, mid=98, [mid]=3098, too low,  lo:=99
Step 7, lo=99, hi=99, mid=99, [mid]=3099, found!
+2

Source: https://habr.com/ru/post/1599162/


All Articles