Skip to content
Closed
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
22 changes: 22 additions & 0 deletions searching_algorithms/binary_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class BinarySearch {
public static int binarySearch(int[] arr, int key) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

public static void main(String[] args) {
int[] arr = {5, 15, 25, 35, 45};
int key = 35;
int result = binarySearch(arr, key);
System.out.println(result == -1 ? "Not found" : "Found at index " + result);
}
}
34 changes: 34 additions & 0 deletions searching_algorithms/exponential_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.Arrays;

public class ExponentialSearch {
public static int binarySearch(int[] arr, int left, int right, int key) {
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

public static int exponentialSearch(int[] arr, int key) {
if (arr[0] == key)
return 0;

int i = 1;
while (i < arr.length && arr[i] <= key)
i = i * 2;

return binarySearch(arr, i / 2, Math.min(i, arr.length - 1), key);
}

public static void main(String[] args) {
int[] arr = {2, 4, 8, 16, 32, 64, 128};
int key = 64;
int result = exponentialSearch(arr, key);
System.out.println(result == -1 ? "Not found" : "Found at index " + result);
}
}
29 changes: 29 additions & 0 deletions searching_algorithms/interpolation_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class InterpolationSearch {
public static int interpolationSearch(int[] arr, int key) {
int low = 0, high = arr.length - 1;

while (low <= high && key >= arr[low] && key <= arr[high]) {
if (low == high) {
return (arr[low] == key) ? low : -1;
}

int pos = low + ((key - arr[low]) * (high - low)) / (arr[high] - arr[low]);

if (arr[pos] == key)
return pos;

if (arr[pos] < key)
low = pos + 1;
else
high = pos - 1;
}
return -1;
}

public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int key = 30;
int result = interpolationSearch(arr, key);
System.out.println(result == -1 ? "Not found" : "Found at index " + result);
}
}
31 changes: 31 additions & 0 deletions searching_algorithms/jump_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class JumpSearch {
public static int jumpSearch(int[] arr, int key) {
int n = arr.length;
int step = (int)Math.floor(Math.sqrt(n));
int prev = 0;

while (arr[Math.min(step, n) - 1] < key) {
prev = step;
step += (int)Math.floor(Math.sqrt(n));
if (prev >= n)
return -1;
}

while (arr[prev] < key) {
prev++;
if (prev == Math.min(step, n))
return -1;
}

if (arr[prev] == key)
return prev;
return -1;
}

public static void main(String[] args) {
int[] arr = {2, 4, 6, 8, 10, 12, 14, 16};
int key = 10;
int result = jumpSearch(arr, key);
System.out.println(result == -1 ? "Not found" : "Found at index " + result);
}
}
16 changes: 16 additions & 0 deletions searching_algorithms/linear_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class LinearSearch {
public static int linearSearch(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key)
return i;
}
return -1;
}

public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int key = 30;
int result = linearSearch(arr, key);
System.out.println(result == -1 ? "Not found" : "Found at index " + result);
}
}
26 changes: 26 additions & 0 deletions searching_algorithms/ternary_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class TernarySearch {
public static int ternarySearch(int[] arr, int left, int right, int key) {
if (right >= left) {
int mid1 = left + (right - left) / 3;
int mid2 = right - (right - left) / 3;

if (arr[mid1] == key) return mid1;
if (arr[mid2] == key) return mid2;

if (key < arr[mid1])
return ternarySearch(arr, left, mid1 - 1, key);
else if (key > arr[mid2])
return ternarySearch(arr, mid2 + 1, right, key);
else
return ternarySearch(arr, mid1 + 1, mid2 - 1, key);
}
return -1;
}

public static void main(String[] args) {
int[] arr = {1, 4, 7, 10, 14, 18, 22};
int key = 18;
int result = ternarySearch(arr, 0, arr.length - 1, key);
System.out.println(result == -1 ? "Not found" : "Found at index " + result);
}
}