Skip to main content

Arrays in Java: (Part 7)

>

Arrays in Java: (Part 7)

One Dimensional Array Binary Search:

10. Binary Search in Java (for integers)
11. Binary Search in Java (for Strings)

  • Example: Binary Search in Java (for Integers)
    import java.util.Scanner;
    class search
    {
    public static void main(String args[])
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Number of elements:");
    int n = scan.nextInt();
    int array[] = new int[n];
    System.out.println("enter " + n + " values");
    for (int i=0; i<n; i++){
    array[i] = scan.nextInt();
    }
    System.out.println("Enter key or value to search");
    int key = scan.nextInt();
    int first = 0;
    int last = n-1;
    int middle = (first+last)/2;
    while(first<=last)
    {
    if (array[middle]<key) {
    first=middle+1;
    }
    else if (array[middle]==key) {
    System.out.println(key + " found at location " + (middle+1));
    break;
    }
    else{
    last=middle-1;
    }
    middle=(first+last)/2;
    }
    if (first>last)
    System.out.println(key + " is not present in the array");
    }
    }
    view raw test.java hosted with ❤ by GitHub
  • Example: Binary Search in Java (for Strings)
    import java.util.Scanner;
    class search {
    public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Number of elements:");
    int n = scan.nextInt();
    String array[] = new String[n];
    System.out.println("enter " + n + " values");
    for (int i=0; i<n; i++){
    array[i] = scan.next();
    }
    System.out.println("Enter key or value to search");
    String key = scan.next();
    int first = 0;
    int last = n-1;
    int middle = (first+last)/2;
    while(first<=last)
    {
    if (array[middle].compareTo(key)<0){
    first=middle+1;
    }
    else if (array[middle].compareTo(key)==0)
    {
    System.out.println(key + " found at location " + (middle+1));
    break;
    }
    else{
    last=middle-1;
    }
    middle=(first+last)/2;
    }
    if (first>last)
    System.out.println(key + " is not present in the array");
    }
    }
    view raw pattern.java hosted with ❤ by GitHub

Comments

Post a Comment

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Character Class in Java (Part 2)

> The Character Class: (Part 1) Methods of Character class: Example: Method -> isDigit() [validating name] Example: Method -> isLetter() [validating mobile number] Example: Method -> isLetterOrDigit() [validating user_id / username] Example: Method -> isLowerCase() Example: Method -> isUpperCase() Example: Method -> toUpperCase() and toLowerCase() Click HERE for the list of Methods of Character class. Click HERE for the ASCII Table for different Characters