Set 4 - Search
Program for Linear Search
private static int method1(int[] arr, int k) {
int index = -1;
for(int i=0; i<arr.length; i++) {
if (arr[i] == k) {
index = i;
break;
}
}
return index;
}
// BEST CASE TIME COMPLEXITY O(1)
// AVERAGE CASE TIME COMPLEXITY O(N)
// WORST-CASE TIME COMPLEXITY O(N)
// SPACE COMPLEXITY O(1)Program for Binary Search
Method 1: Iterative Method
Method 2: Recursive Method
Method 3: Inbuild Method
Method 4: Binary Search in Collection List
Last updated