Java Program to Perform Uniform Binary Search

This is a java program to perform uniform binary search technique. It uses a lookup table to update a single array index, rather than taking the midpoint of an upper and a lower bound on each iteration; therefore, it is optimized for architectures (such as Knuth’s MIX) on which a table look-up is generally faster than an addition and a shift, and many searches will be performed on the same array, or on several arrays of the same length.

Here is the source code of the Java Program to Perform Uniform Binary Search. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to perform uniform binary search.
import java.util.Random;
import java.util.Scanner;
 
public class Uniform_Binary_Search 
{
    static int N = 10;
    static int[] sequence = new int[N];
    static int[] delta = new int[42];
 
    public static void sort() 
    {
        int i, j, temp;
        for (i = 1; i < N; i++) 
        {
            j = i;
            temp = sequence[i];
            while (j > 0 && temp < sequence[j - 1]) 
            {
                sequence[j] = sequence[j - 1];
                j = j - 1;
            }
            sequence[j] = temp;
        }
    }
 
    public static void make_delta(int N) 
    {
        System.out.println();
        int power = 1;
        int i = 0;
        do 
        {
            int half = power;
            power <<= 1;
            delta[i] = (N + half) / power;
        } while (delta[i++] != 0);
    }
 
    public static int unisearch(int key) 
    {
        int i = delta[0] - 1; /* midpoint of array */
        int d = 0;
 
        while (true) 
        {
            if (key == sequence[i])
                return i;
            else if (delta[d] == 0)
                return -1;
            else 
            {
                if (key < sequence[i])
                    i -= delta[++d];
                else
                    i += delta[++d];
            }
        }
    }
 
    public static void main(String args[]) 
    {
        Random random = new Random();
 
        for (int i = 0; i < N; i++)
            sequence[i] = Math.abs(random.nextInt(100));
        System.out.println("The sequence is :");
        sort();
        for (int i = 0; i < N; i++)
            System.out.print(sequence[i] + " ");
        //sort();
        make_delta(N);
 
        System.out.println("Enter the element to be searched: ");
        Scanner sc = new Scanner(System.in);
        int key = sc.nextInt();
        int p = unisearch(key);
        if (p > 0)
            System.out.println("Element found at position " + p);
        else
            System.out.println("Element doesn't exist");
        sc.close();
    }
}

Output:

$ javac Uniform_Binary_Search.java
$ java Uniform_Binary_Search
 
The sequence is :
12 13 20 24 27 32 63 64 74 82 
Enter the element to be searched: 
24
Element found at position 3
 
The sequence is :
0 30 31 32 37 48 51 58 78 87 
Enter the element to be searched: 
98
Element doesn't exist

Related posts:

Java Program to Implement Circular Doubly Linked List
Most commonly used String methods in Java
Spring Boot - Bootstrapping
XML-Based Injection in Spring
@DynamicUpdate with Spring Data JPA
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Introduction to Using FreeMarker in Spring MVC
Java Program to Generate Randomized Sequence of Given Range of Numbers
Creating a Generic Array in Java
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Represent Graph Using Adjacency Matrix
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Giới thiệu Google Guice – Binding
Hướng dẫn Java Design Pattern – Adapter
Java Program to Perform integer Partition for a Specific Case
Using Spring @ResponseStatus to Set HTTP Status Code
How to Read a File in Java
Java Program to Implement Gale Shapley Algorithm
Redirect to Different Pages after Login with Spring Security
Java Program to Implement Variable length array
Java Program to Find the Vertex Connectivity of a Graph
Spring Boot - Internationalization
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Implement Ternary Search Tree
Java Program to Compute Discrete Fourier Transform Using Naive Approach
A Guide to ConcurrentMap
Exploring the New Spring Cloud Gateway
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
So sánh HashMap và Hashtable trong Java
Java Program to Perform Sorting Using B-Tree
Java Program to Implement Affine Cipher