PLEASE BRIEFLY DISCUSS ABOUT THE DEFINITIONS AND
SAMPLE CODES FOR
BINARY SEARCH AND TARGET SEARCH
Binary Search:
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
We basically ignore half of the elements just after one comparison.
# Python Program for recursive binary search.
# Returns index of x in arr if present, else -1
def binarySearch (arr, l, r, x):
# Check base case
if r >= l:
mid = l + (r - l)/2
# If element is present at the
middle itself
if arr[mid] == x:
return mid
# If element is smaller than mid,
then it
# can only be present in left
subarray
elif arr[mid] > x:
return
binarySearch(arr, l, mid-1, x)
# Else the element can only be
present
# in right subarray
else:
return
binarySearch(arr, mid + 1, r, x)
else:
# Element is not present in the
array
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print "Element is present at index % d" % result
else:
print "Element is not present in array"
-------------------------------------------------------------------------------------------------------------------------
# Python code to implement iterative Binary Search.
# It returns location of x in given array array
# if present, else returns -1
def binarySearch(arr, l, r, x):
while l <= r:
mid = l + (r - l)/2;
# Check if x is present at
mid
if arr[mid] == x:
return mid
# If x is greater, ignore left
half
elif arr[mid] < x:
l = mid + 1
# If x is smaller, ignore right
half
else:
r = mid -
1
# If we reach here, then the element
# was not present
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print "Element is present at index % d" % result
else:
print "Element is not present in array"
---------------------------------------------------------------------------------------------------------------------------------------
Target Search :
Given a target position on infinite number line, i.e -infinity to +infinity. Starting form 0 you have to reach the target by moving as described : In ith move you can take i steps forward or backward. Find the minimum number of moves require to reach the target.
# Python 3 program to find minimum moves to reach target if we can move i steps in i-th move.
def reachTarget(target) :
# Handling negatives by symmetry
target = abs(target)
# Keep moving while sum is
# smaller or difference is odd.
sum = 0
step = 0
while (sum < target or (sum - target) % 2 != 0)
:
step = step + 1
sum = sum + step
return step
target = 5
print(reachTarget(target))
PLEASE BRIEFLY DISCUSS ABOUT THE DEFINITIONS AND SAMPLE CODES FOR BINARY SEARCH AND TARGET SEARCH
PLEASE GIVE BRIEF EXPLANATIONS AND EXAMPLE CODES ON BINARY AND TARGET SEARCH AND HOW THIS COULD BE APPLIED ON THE RF CIRCUIT
What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result? int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; (Using Java code please)
Discuss the procedure to form a binary search tree for a list of items. Please discuss in details !!! More details...
What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result? int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...
## Codes must be in Python ## In a binary search tree What is worst case time complexity of the binary_search function? Provide an example binary search tree that exhibits worst case running time of binary_search function Write a function that prints elements in binary search tree in order
Write a binary search function that, instead of returning -1 when the target value is not in the list, raises a TargetNotFound exception (you'll need to define this exception class). Otherwise it should function normally. Name this function bin_except. The file must be named: bin_except.py PYTHON 3 ONLY
Python 3 Write a binary search function that, instead of returning -1 when the target value is not in the list, raises a TargetNotFound exception (you'll need to define this exception class). Otherwise it should function normally. Name this function bin_except. The file must be named: bin_except.py
JAVA: Explain the advantages and disadvantages of binary search tree structures. Discuss ways to quantify performance.
Given the following code find the worst case time complexity binary search (target: integer, a[1..n ]: ascending integers) k =1 j =n loop when (k is less than j) m =floor((k+j)/2) if (target is larger than the element at m) then k = m+1 else j = m endloop if (target equals element at k) then location=k else location =0