2

I am using List for dynamically storing the values, but I have to specify the size of List before taking input in the Binary search program. I want help in that I don't want to take size as an input.

I have tried using length but that doesn't work, someone suggested me to use size(), but I don't know how to use it.

import java.util.*;

class Binary
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        System.out.println("Enter size of array");
        int n=s.nextInt();
        System.out.println("Enter array elements in ascending order");
        List<Integer> L=new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            int e=s.nextInt();
            L.add(e);
        }
        System.out.println("Enter the element you want to search");
        int h=s.nextInt();
        int left=L.get(0);
        int right=L.get(n-1);
        while(left<=right)
        {
            if(h<=right){
                int m=(left+right)/2;

                if(m==h)
                {
                    System.out.println("Element found at index:"+L.indexOf(m)+" starting from 0");
                    return;
                }
                if(m>h)
                {
                    right=m-1;
                }
                if(m<h)
                {
                    left=m+1;
                }
            }
            else{
                System.out.println("Element not present");
            }   
        }
    }
}

Now I am expecting the user to enter elements till he desires and then to find index of element using Binary Search

jhamon
  • 3,603
  • 4
  • 26
  • 37
Rohit Mittal
  • 64
  • 2
  • 8
  • If you don't want the user to specify the number of elements he wants to enter, how do you want him to indicate that he is done with entering all elements? – Turamarth Oct 01 '19 at 14:09
  • You can read numbers until user enters an escape number. Then use `L.get(L.size() - 1);` instead of `L.get(n-1);` – SomeFire Oct 01 '19 at 14:18
  • If we declare 0 as an escape element then the user will no be able to enter 0 in between, so we cannot use any number as an escape element.@SomeFire – Rohit Mittal Oct 02 '19 at 05:16
  • how about a string, `end`? You'll need to either multiline your number input, or you can treat a single line of input as the array of numbers and split it by whitespace or some other delimiter (thus eliminating a need for the end of input, it'll be the enter key) – Rogue Oct 02 '19 at 16:27

1 Answers1

0

Your question is really a duplicate of How to terminate Scanner when input is complete?.

But here are a few pointers to get your program working.

This part of the code is incorrect.

    int left=L.get(0);
    int right=L.get(n-1);

You need to be setting the left and right to be the bounds of the search - not the values the user entered into those positions.

    int left=0;
    int right=L.size() - 1;

Then when you need to compare the values the user entered.

    if (L.get(m) < h)

L.indexOf(m) doesn't really make sense. It would work if you had L.indexOf(L.get(m)) but that is the same as just m.

    System.out.println("Element found at index:" + m + " starting from 0");
fedup
  • 1,209
  • 11
  • 26