DEV Community

Cover image for Searching Algorithms with Java
Shubham Tiwari
Shubham Tiwari

Posted on

Searching Algorithms with Java

Hello everyone today i will be discussing Searching Algorithms for beginners.I will discuss few algorithms with example in Java.

Searching is a fundamental operation in computer science and is utilized in various applications and scenarios. Whether you need to find a specific element in a collection of data or determine if a value exists, searching algorithms play a crucial role in solving these problems efficiently. In this article, we will explore different searching algorithms and provide examples in Java to illustrate their implementation.

1. Linear Search:

Linear search, also known as sequential search, is the simplest searching algorithm. It sequentially checks each element in a collection until the target value is found or the end of the collection is reached.

public static int linearSearch(int[] arr, int target) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == target) {
            return i; // target found at index i
        }
    }
    return -1; // target not found
}
Enter fullscreen mode Exit fullscreen mode

2. Binary Search:

Binary search is a more efficient searching algorithm that requires a sorted collection of elements. It repeatedly divides the collection in half, eliminating the half where the target value cannot be present.

public static int binarySearch(int[] arr, int target) {
    int left = 0;
    int right = arr.length - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;

        if (arr[mid] == target) {
            return mid; // target found at index mid
        }

        if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    return -1; // target not found
}
Enter fullscreen mode Exit fullscreen mode

3. Interpolation Search:

Interpolation search is a variant of binary search that performs better on uniformly distributed sorted data. It uses an estimation to guess the probable location of the target value within the range.

public static int interpolationSearch(int[] arr, int target) {
    int left = 0;
    int right = arr.length - 1;

    while (left <= right && target >= arr[left] && target <= arr[right]) {
        if (left == right) {
            if (arr[left] == target) {
                return left; // target found at index left
            }
            return -1; // target not found
        }

        int pos = left + (((right - left) / (arr[right] - arr[left])) * (target - arr[left]));

        if (arr[pos] == target) {
            return pos; // target found at index pos
        }

        if (arr[pos] < target) {
            left = pos + 1;
        } else {
            right = pos - 1;
        }
    }

    return -1; // target not found
}
Enter fullscreen mode Exit fullscreen mode

4. Hashing and HashMap:

Hashing is a technique that maps data elements to specific indices in an array called a hash table. Java provides a built-in HashMap class that uses hashing to achieve fast searching and retrieval.

import java.util.HashMap;

public class HashMapExample {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("Alice", 25);
        map.put("Bob", 30);
        map.put("Charlie", 35);

        System.out.println(map.get("Bob")); // Output: 30
    }
}
Enter fullscreen mode Exit fullscreen mode

Searching algorithms are essential tools for every programmer. Understanding their characteristics and choosing the appropriate algorithm based on the problem at hand can significantly impact the efficiency of your code. The examples provided in Java illustrate the implementation of different searching algorithms, enabling you to apply them in your projects and applications.

THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me with some donation at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/website-components-you-should-know-25nm

https://dev.to/shubhamtiwari909/smooth-scrolling-with-js-n56

https://dev.to/shubhamtiwari909/swiperjs-3802

https://dev.to/shubhamtiwari909/custom-tabs-with-sass-and-javascript-4dej

Top comments (0)