Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added different types of searching algorithm #2904

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Java Data structures/SearchingAlgorithm/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
30 changes: 30 additions & 0 deletions Java Data structures/SearchingAlgorithm/ExponentialSearch.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
37 changes: 37 additions & 0 deletions Java Data structures/SearchingAlgorithm/InterpolationSearch.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
36 changes: 36 additions & 0 deletions Java Data structures/SearchingAlgorithm/JumpSearch.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
24 changes: 24 additions & 0 deletions Java Data structures/SearchingAlgorithm/LinearSearch.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}