From 8956d235d8673304be4240c23a9e2a6a10dfeebd Mon Sep 17 00:00:00 2001 From: Vaishnavi Sanjay Ahire <154779103+VaishnaviAhire@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:00:15 +0530 Subject: [PATCH] Added different types of searching algorithm --- .../SearchingAlgorithm/BinarySearch.java | 40 +++++++++++++++++++ .../SearchingAlgorithm/ExponentialSearch.java | 30 ++++++++++++++ .../InterpolationSearch.java | 37 +++++++++++++++++ .../SearchingAlgorithm/JumpSearch.java | 36 +++++++++++++++++ .../SearchingAlgorithm/LinearSearch.java | 24 +++++++++++ 5 files changed, 167 insertions(+) create mode 100644 Java Data structures/SearchingAlgorithm/BinarySearch.java create mode 100644 Java Data structures/SearchingAlgorithm/ExponentialSearch.java create mode 100644 Java Data structures/SearchingAlgorithm/InterpolationSearch.java create mode 100644 Java Data structures/SearchingAlgorithm/JumpSearch.java create mode 100644 Java Data structures/SearchingAlgorithm/LinearSearch.java diff --git a/Java Data structures/SearchingAlgorithm/BinarySearch.java b/Java Data structures/SearchingAlgorithm/BinarySearch.java new file mode 100644 index 000000000..01f5334f0 --- /dev/null +++ b/Java Data structures/SearchingAlgorithm/BinarySearch.java @@ -0,0 +1,40 @@ +import java.util.Arrays; + +public class BinarySearch { + // Function to perform binary search + public static int binarySearch(int[] array, int target) { + int left = 0; + int right = array.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; // Calculate mid index + + // Check if target is at mid + if (array[mid] == target) { + return mid; // Return the index if the target is found + } + // If target is greater, ignore the left half + else if (array[mid] < target) { + left = mid + 1; + } + // If target is smaller, ignore the right half + else { + right = mid - 1; + } + } + return -1; // Return -1 if the target is not found + } + + public static void main(String[] args) { + int[] array = {3, 5, 7, 8, 10, 12, 15}; + int target = 10; + + int result = binarySearch(array, target); + + if (result == -1) { + System.out.println("Element not found in the array."); + } else { + System.out.println("Element found at index: " + result); + } + } +} diff --git a/Java Data structures/SearchingAlgorithm/ExponentialSearch.java b/Java Data structures/SearchingAlgorithm/ExponentialSearch.java new file mode 100644 index 000000000..420240ee2 --- /dev/null +++ b/Java Data structures/SearchingAlgorithm/ExponentialSearch.java @@ -0,0 +1,30 @@ +import java.util.Arrays; + +public class ExponentialSearch { + // Exponential search function + public static int exponentialSearch(int[] array, int target) { + if (array[0] == target) { + return 0; + } + + int i = 1; + while (i < array.length && array[i] <= target) { + i *= 2; + } + + // Binary search within the found range + return Arrays.binarySearch(array, i / 2, Math.min(i, array.length), target); + } + + public static void main(String[] args) { + int[] array = {3, 5, 7, 9, 10, 13, 15, 18, 20, 22, 25}; + int target = 18; + + int result = exponentialSearch(array, target); + if (result < 0) { + System.out.println("Element not found in the array."); + } else { + System.out.println("Element found at index: " + result); + } + } +} diff --git a/Java Data structures/SearchingAlgorithm/InterpolationSearch.java b/Java Data structures/SearchingAlgorithm/InterpolationSearch.java new file mode 100644 index 000000000..4c0410e08 --- /dev/null +++ b/Java Data structures/SearchingAlgorithm/InterpolationSearch.java @@ -0,0 +1,37 @@ +public class InterpolationSearch { + public static int interpolationSearch(int[] array, int target) { + int low = 0; + int high = array.length - 1; + + while (low <= high && target >= array[low] && target <= array[high]) { + // Estimate the position using the interpolation formula + int pos = low + ((target - array[low]) * (high - low) / (array[high] - array[low])); + + // If target is found + if (array[pos] == target) { + return pos; + } + // If target is larger, it is in the right subarray + if (array[pos] < target) { + low = pos + 1; + } + // If target is smaller, it is in the left subarray + else { + high = pos - 1; + } + } + return -1; // Target not found + } + + public static void main(String[] args) { + int[] array = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24}; + int target = 18; + + int result = interpolationSearch(array, target); + if (result == -1) { + System.out.println("Element not found in the array."); + } else { + System.out.println("Element found at index: " + result); + } + } +} diff --git a/Java Data structures/SearchingAlgorithm/JumpSearch.java b/Java Data structures/SearchingAlgorithm/JumpSearch.java new file mode 100644 index 000000000..8992dde0a --- /dev/null +++ b/Java Data structures/SearchingAlgorithm/JumpSearch.java @@ -0,0 +1,36 @@ +public class JumpSearch { + public static int jumpSearch(int[] array, int target) { + int n = array.length; + int step = (int) Math.sqrt(n); // Calculate block size + + int prev = 0; + // Jump to the next block until we find a block where `target` could exist + while (array[Math.min(step, n) - 1] < target) { + prev = step; + step += (int) Math.sqrt(n); + if (prev >= n) { + return -1; // Target is not present in the array + } + } + + // Perform linear search within the block + for (int i = prev; i < Math.min(step, n); i++) { + if (array[i] == target) { + return i; + } + } + return -1; // Target is not present + } + + public static void main(String[] args) { + int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17}; + int target = 15; + + int result = jumpSearch(array, target); + if (result == -1) { + System.out.println("Element not found in the array."); + } else { + System.out.println("Element found at index: " + result); + } + } +} diff --git a/Java Data structures/SearchingAlgorithm/LinearSearch.java b/Java Data structures/SearchingAlgorithm/LinearSearch.java new file mode 100644 index 000000000..52319b9af --- /dev/null +++ b/Java Data structures/SearchingAlgorithm/LinearSearch.java @@ -0,0 +1,24 @@ +public class LinearSearch { + // Function to perform linear search + public static int linearSearch(int[] array, int target) { + for (int i = 0; i < array.length; i++) { + if (array[i] == target) { + return i; // Return the index if the target is found + } + } + return -1; // Return -1 if the target is not found + } + + public static void main(String[] args) { + int[] array = {5, 8, 12, 7, 3, 15, 10}; + int target = 7; + + int result = linearSearch(array, target); + + if (result == -1) { + System.out.println("Element not found in the array."); + } else { + System.out.println("Element found at index: " + result); + } + } +}