Tuesday, April 16, 2013

Binary Search in Recursion : JAVA OOP

Write Java Source Code: Binary Search in Recursion

Sample Output 1:
Enter the size of the array: 5
Enter 5 numbers: 2 1 3 5 4
The sorted numbers are:
1 2 3 4 5
Enter the number you want to search: 3
The search number is on the index 2
Source Code:

//Java Class
public class binarySearch //Example of Java Class
{
    //biSearch is an example of a method or a function
    public int binSearch(int[] arr, int fIndex, int lIndex,int search)
    {
int middle = (fIndex + (lIndex - fIndex) / 2);
        if(fIndex<lIndex ){
            if (search == arr[middle]){
                return middle;
            }
            else if(search < arr[middle]){
                if(search == arr[0])
                    return 0;
                return binSearch(arr, fIndex, middle, search);
            }
            else if(search > arr[middle]){
                if(search == arr[middle+1])
                    return middle + 1;
                return binSearch(arr, middle+1, lIndex, search);
            }
        }
       return -1;
    }
//this is also a class method
 public void sort(int[] arr)
{
       for(int i=0; i<arr.length; i++)
        {
            for(int j=i+1; j<arr.length; j++ )
            {
                if(arr[i] > arr[j])
                {
                    int temp = arr[j];
                    arr[j]=arr[i];
                    arr[i]= temp;
                }
            }
        }
       for(int i=0; i<arr.length; i++)
       {
           System.out.print(arr[i] + " ");
       }
}
}
//main class
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
         Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];
        System.out.print("Enter "+ n +" numbers: ");
        int middle;
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }
        binarySearch access = new binarySearch(); //this is how to instantiate an object to access a class
        System.out.println("The sorted numbers are: ");
        access.sort(x);//this is how to access a method
        System.out.println();
         
        System.out.print("Enter the number you want to search: ");
        int value = input.nextInt();
        System.out.print("The search number is on the index ");
        System.out.print(access.binSearch(x, 0, x.length-1, value)); //how to access a class
    }
}

9 comments:

  1. Mohammad Ashraful Hasan Sobuj
    My Batch- 46th
    My Department :CSE
    My ID 201420656


    public class binarySearch
    {
    public int binSearch(int[] a, int fIndex, int lIndex,int search);
    int m = (fIndex + (lIndex - fIndex) / 2);
    if(fIndex a[m]);
    {
    if(search == a[m+1]);
    return m + 1;
    return binSearch(a, m+1, lIndex, search);
    }
    }
    return -1;
    }
    public void sort(int[] a);
    {
    for(int i=0; i a[j]);
    int temp = a[j];
    a[j]=a[i];
    a[i]= temp;
    }
    }
    }
    for(int i=0; i<a.length; i++);
    {
    System.out.print(a[i] + " ");
    }
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args);
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the array: ");
    int n = input.nextInt();
    int[] x = new int[n];
    System.out.print("Enter "+ n +" numbers: ");
    int m;
    for(int i=0; i<n; i++);
    {
    x[i] = input.nextInt();
    }
    binarySearch access = new binarySearch();
    System.out.println("The sorted numbers are: ");
    access.sort(x);
    System.out.println();
    System.out.print("Enter the number you want to search: ");
    int value = input.nextInt();
    System.out.print("The search number is on the index ");
    System.out.print(access.binSearch(x, 0, x.length-1, value));
    }
    }

    ReplyDelete
  2. sultana razia khan
    My Batch- 46th
    My Department :CSE
    My ID 201420715


    public class binarySearch
    {
    public int binSearch(int[] a, int fIndex, int lIndex,int search);
    int m = (fIndex + (lIndex - fIndex) / 2);
    if(fIndex a[m]);
    {
    if(search == a[m+1]);
    return m + 1;
    return binSearch(a, m+1, lIndex, search);
    }
    }
    return -1;
    }
    public void sort(int[] a);
    {
    for(int i=0; i a[j]);
    int temp = a[j];
    a[j]=a[i];
    a[i]= temp;
    }
    }
    }
    for(int i=0; i<a.length; i++);
    {
    System.out.print(a[i] + " ");
    }
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args);
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the array: ");
    int n = input.nextInt();
    int[] x = new int[n];
    System.out.print("Enter "+ n +" numbers: ");
    int m;
    for(int i=0; i<n; i++);
    {
    x[i] = input.nextInt();
    }
    binarySearch access = new binarySearch();
    System.out.println("The sorted numbers are: ");
    access.sort(x);
    System.out.println();
    System.out.print("Enter the number you want to search: ");
    int value = input.nextInt();
    System.out.print("The search number is on the index ");
    System.out.print(access.binSearch(x, 0, x.length-1, value));
    }
    }

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Sharmin Akter
    My Batch- 46th
    My Department :CSE
    My ID 201420769


    public class binarySearch
    {
    public int binSearch(int[] a, int fIndex, int lIndex,int search);
    int m = (fIndex + (lIndex - fIndex) / 2);
    if(fIndex a[m]);
    {
    if(search == a[m+1]);
    return m + 1;
    return binSearch(a, m+1, lIndex, search);
    }
    }
    return -1;
    }
    public void sort(int[] a);
    {
    for(int i=0; i a[j]);
    int temp = a[j];
    a[j]=a[i];
    a[i]= temp;
    }
    }
    }
    for(int i=0; i<a.length; i++);
    {
    System.out.print(a[i] + " ");
    }
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args);
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the array: ");
    int n = input.nextInt();
    int[] x = new int[n];
    System.out.print("Enter "+ n +" numbers: ");
    int m;
    for(int i=0; i<n; i++);
    {
    x[i] = input.nextInt();
    }
    binarySearch access = new binarySearch();
    System.out.println("The sorted numbers are: ");
    access.sort(x);
    System.out.println();
    System.out.print("Enter the number you want to search: ");
    int value = input.nextInt();
    System.out.print("The search number is on the index ");
    System.out.print(access.binSearch(x, 0, x.length-1, value));
    }
    }

    ReplyDelete
  5. Name : jalis Mahmud
    ID : 201420762
    Batch : 46th




    public class binarySearch
    {
    public int binSearch(int[] a, int fIndex, int lIndex,int search);
    int x = (fIndex + (lIndex - fIndex) / 3);
    if(fIndex a[x]);
    {
    if(search == a[x+2]);
    return x + 2;
    return binSearch(a, x+2, lIndex, search);
    }
    }
    return -2;
    }
    public void sort(int[] a);
    {
    for(int i=0; i a[j]);
    int temp = a[j];
    a[j]=a[i];
    a[i]= temp;
    }
    }
    }
    for(int i=0; i<a.length; i++);
    {
    System.out.print(a[i] + " ");
    }
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args);
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the array: ");
    int n = input.nextInt();
    int[] x = new int[n];
    System.out.print("Enter "+ n +" numbers: ");
    int m;
    for(int i=0; i<n; i++);
    {
    x[i] = input.nextInt();
    }
    binarySearch access = new binarySearch();
    System.out.println("The sorted numbers are: ");
    access.sort(x);
    System.out.println();
    System.out.print("Enter the number you want to search: ");
    int value = input.nextInt();
    System.out.print("The search number is on the index ");
    System.out.print(access.binSearch(x, 0, x.length-1, value));
    }
    }

    ReplyDelete
  6. ID-201420406
    Batch-46th

    public class binarySearch
    {
    public int binSearch(int[] a, int fIndex, int lIndex,int search);
    int m = (fIndex + (lIndex - fIndex) / 2);
    if(fIndex a[m]);
    {
    if(search == a[m+1]);
    return m + 1;
    return binSearch(a, m+1, lIndex, search);
    }
    }
    return -1;
    }
    public void sort(int[] a);
    {
    for(int i=0; i a[j]);
    int temp = a[j];
    a[j]=a[i];
    a[i]= temp;
    }
    }
    }
    for(int i=0; i<a.length; i++);
    {
    System.out.print(a[i] + " ");
    }
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args);
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the array: ");
    int n = input.nextInt();
    int[] x = new int[n];
    System.out.print("Enter "+ n +" numbers: ");
    int m;
    for(int i=0; i<n; i++);
    {
    x[i] = input.nextInt();
    }
    binarySearch access = new binarySearch();
    System.out.println("The sorted numbers are: ");
    access.sort(x);
    System.out.println();
    System.out.print("Enter the number you want to search: ");
    int value = input.nextInt();
    System.out.print("The search number is on the index ");
    System.out.print(access.binSearch(x, 0, x.length-1, value));
    }
    }

    ReplyDelete

  7. import java.util.Scanner;

    public class binarySearch
    public int binSearch(int[] arr, int fIndex, int lIndex,int search)
    {

    int middle = (fIndex + (lIndex - fIndex) / 2);

    if(fIndex arr[middle]){
    if(search == arr[middle+1])
    return middle + 1;
    return binSearch(arr, middle+1, lIndex, search);
    }

    }
    return -1;
    }

    public void sort(int[] arr)
    {
    for(int i=0; i arr[j])
    {
    int temp = arr[j];
    arr[j]=arr[i];
    arr[i]= temp;
    }
    }
    }

    for(int i=0; i<arr.length; i++)
    {
    System.out.print(arr[i] + " ");
    }
    }
    }

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter the size of the array: ");
    int n = input.nextInt();
    int[] x = new int[n];

    System.out.print("Enter "+ n +" numbers: ");
    int middle;
    for(int i=0; i<n; i++)
    {
    x[i] = input.nextInt();
    }

    binarySearch access = new binarySearch();
    System.out.println("The sorted numbers are: ");
    access.sort(x);
    System.out.println();

    System.out.print("Enter the number you want to search: ");
    int value = input.nextInt();

    System.out.print("The search number is on the index ");
    System.out.print(access.binSearch(x, 0, x.length-1, value));
    }
    }

    ReplyDelete
  8. ID-201420763
    Batch-46th

    public class binarySearch
    {
    public int binSearch(int[] a, int fIndex, int lIndex,int search);
    int m = (fIndex + (lIndex - fIndex) / 2);
    if(fIndex a[m]);
    {
    if(search == a[m+1]);
    return m + 1;
    return binSearch(a, m+1, lIndex, search);
    }
    }
    return -1;
    }
    public void sort(int[] a);
    {
    for(int i=0; i a[j]);
    int temp = a[j];
    a[j]=a[i];
    a[i]= temp;
    }
    }
    }
    for(int i=0; i<a.length; i++);
    {
    System.out.print(a[i] + " ");
    }
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args);
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the array: ");
    int n = input.nextInt();
    int[] x = new int[n];
    System.out.print("Enter "+ n +" numbers: ");
    int m;
    for(int i=0; i<n; i++);
    {
    x[i] = input.nextInt();
    }
    binarySearch access = new binarySearch();
    System.out.println("The sorted numbers are: ");
    access.sort(x);
    System.out.println();
    System.out.print("Enter the number you want to search: ");
    int value = input.nextInt();
    System.out.print("The search number is on the index ");
    System.out.print(access.binSearch(x, 0, x.length-1, value));
    }
    }

    ReplyDelete
  9. Once a person has decided which website or websites to work with, he or she will need to determine how much money should be invested in binary options.my company

    ReplyDelete