Data Structure & Algorithm in Java

⌘K
  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Searching and Hashing
  5. Efficiency of Searching Algorithms

Efficiency of Searching Algorithms

In Data Structures and Algorithms (DSA), the efficiency of searching algorithms is crucial for determining how quickly a search operation can locate a specific element in a dataset.

Here are some key searching algorithms along with their typical efficiency:

  1. Linear Search:
    • Linear search has a time complexity of O(n) where n is the number of elements in the dataset. It sequentially checks each element in the list until the desired element is found.
  2. Binary Search:
    • Binary search is a more efficient algorithm compared to linear search. It has a time complexity of O(log n) where n is the number of elements in a sorted dataset. Binary search works by repeatedly dividing the search interval in half until the element is found.
  3. Hashing:
    • Hashing allows for constant time search operations on average, i.e., O(1). This is achieved by mapping keys to indices in a data structure called a hash table. However, in the worst case, hashing can have a time complexity of O(n) if there are many collisions.
  4. Binary Search Tree (BST):
    • In a Binary Search Tree, the time complexity for search operations is O(log n) on average, with O(n) being the worst-case scenario. The search is efficient due to the binary search property of the tree.
  5. Ternary Search:
    • Ternary search is a divide-and-conquer algorithm that works on a sorted dataset. It has a time complexity of O(log3 n), where each comparison eliminates roughly one-third of the dataset.
  6. Jump Search:
    • Jump search is an efficient algorithm for sorted datasets. It has a time complexity of O(√n), where n is the number of elements. Jump search works by jumping ahead by fixed steps and then linearly searching for the desired element.
public class LinearSearch {
    public static int linearSearch(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                return i; // Return the index where the target is found
            }
        }
        return -1; // Return -1 if target is not found
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int target = 6;
        int index = linearSearch(arr, target);
        if (index != -1) {
            System.out.println("Element found at index: " + index);
        } else {
            System.out.println("Element not found");
        }
    }
}
public class BinarySearch {
    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; // Return the index where the target is found
            }

            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1; // Return -1 if target is not found
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int target = 6;
        int index = binarySearch(arr, target);
        if (index != -1) {
            System.out.println("Element found at index: " + index);
        } else {
            System.out.println("Element not found");
        }
    }
}

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *