40
loading...
This website collects cookies to deliver better user experience
arr = [2, 12, 15, 11, 7, 19, 45]
7
.package algorithms.searching;
public class LinearSearch {
public static void main(String[] args) {
int[] nums = {2, 12, 15, 11, 7, 19, 45};
int target = 7;
System.out.println(search(nums, target));
}
static int search(int[] nums, int target) {
for (int index = 0; index < nums.length; index++) {
if (nums[index] == target) {
return index;
}
}
return -1;
}
}
def search(nums, target):
for i in range(len(nums)):
if nums[i] == target:
return i
return -1
if __name__ == '__main__':
nums = [2, 12, 15, 11, 7, 19, 45]
target = 7
print(search(nums, target))
O(1)
.O(N)
(the constant being ignored).O(N)
.arr = [2, 12, 15, 17, 27, 29, 45]
7
.package algorithms.searching;
public class BinarySearch {
public static void main(String[] args) {
int[] nums = {2, 12, 15, 17, 27, 29, 45};
int target = 17;
System.out.println(search(nums, target));
}
static int search(int[] nums, int target) {
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (nums[mid] > target)
end = mid - 1;
else if (nums[mid] < target)
start = mid + 1;
else
return mid;
}
return -1;
}
}
def search(nums, target):
start = 0
end = len(nums)-1
while start <= end:
mid = start + (end-start)//2
if nums[mid] > target:
end = mid-1
elif nums[mid] < target:
start = mid+1
else:
return mid
return -1
if __name__ == '__main__':
nums = [2, 12, 15, 17, 27, 29, 45]
target = 17
print(search(nums, target))
O(1)
.O(logN)
O(logN)
Length of array = N
Length of array = N/2
Length of array = (N/2)/2 = N/2^2
Length of array = N/2^k
Length of array = N⁄2k = 1 => N = 2k
=> log2 (N) = log2 (2k) => log2 (N) = k log2 (2)
=> k = log2 (N)
Hence, the time complexity of Binary Search is log2 (N)
end=mid-1
.start=mid+1
if arr[0] < arr[arr.length-1]
array is sorted in ascending order
else
array is sorted in descending order
package algorithms.searching;
public class OrderAgnosticBinarySearch {
public static void main(String[] args) {
int[] nums1 = {-1, 2, 4, 6, 7, 8, 12, 15, 19, 32, 45, 67, 99};
int[] nums2 = {99, 67, 45, 32, 19, 15, 12, 8, 7, 6, 4, 2, -1};
int target = -1;
System.out.println(search(nums1, target));
System.out.println(search(nums2, target));
}
static int search(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;
boolean isAscending = arr[start] < arr[end];
while (start <= end) {
int mid = start + (end - start) / 2;
if (target == arr[mid])
return mid;
if (isAscending) {
if (target < arr[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
} else {
if (target < arr[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
return -1;
}
}
def search(nums, target):
start = 0
end = len(nums)-1
is_ascending = nums[start] < nums[end]
while start <= end:
mid = start + (end-start)//2
if target == nums[mid]:
return mid
if is_ascending:
if target < nums[mid]:
end = mid-1
else:
start = mid+1
else:
if target < nums[mid]:
start = mid+1
else:
end = mid-1
return -1
if __name__ == '__main__':
nums1 = [-1, 2, 4, 6, 7, 8, 12, 15, 19, 32, 45, 67, 99]
nums2 = [99, 67, 45, 32, 19, 15, 12, 8, 7, 6, 4, 2, -1]
target = -1
print(search(nums1, target))
print(search(nums2, target))