-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP29_LinearAndBinarySearch.java
More file actions
54 lines (46 loc) · 1.92 KB
/
Copy pathP29_LinearAndBinarySearch.java
File metadata and controls
54 lines (46 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package programs;
import java.util.Arrays;
/**
* ============================================================
* PROGRAM 29: Linear Search and Binary Search Implementations
* ============================================================
* Problem: WAP to implement:
* 1. Linear Search: O(n) on unsorted arrays
* 2. Binary Search: O(log n) on sorted arrays
* ============================================================
*/
public class P29_LinearAndBinarySearch {
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) return i; // found
}
return -1;
}
public static int binarySearch(int[] sortedArr, int target) {
int low = 0;
int high = sortedArr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2; // avoids integer overflow
if (sortedArr[mid] == target) {
return mid; // found
} else if (sortedArr[mid] < target) {
low = mid + 1; // search right
} else {
high = mid - 1; // search left
}
}
return -1; // not found
}
public static void main(String[] args) {
int[] data = {45, 12, 85, 32, 89, 39, 69, 44};
System.out.println("=== 1. LINEAR SEARCH (Unsorted Array) ===");
System.out.println("Array: " + Arrays.toString(data));
System.out.println("Search for 89 -> index: " + linearSearch(data, 89));
System.out.println("Search for 99 -> index: " + linearSearch(data, 99));
System.out.println("\n=== 2. BINARY SEARCH (Sorted Array) ===");
Arrays.sort(data);
System.out.println("Sorted Array: " + Arrays.toString(data));
System.out.println("Binary Search for 45 -> index: " + binarySearch(data, 45));
System.out.println("Binary Search for 100 -> index: " + binarySearch(data, 100));
}
}