Recursive Algorithm to Find the Minimum Integer in an Array and its SpaceTime Tr
ID: 3724758 • Letter: R
Question
Recursive Algorithm to Find the Minimum Integer in an Array and its SpaceTime Tradeoff Performance Comparison Analysis with an Iterative Algorithm
Design and implement a recursive algorithm to find the minimum integer in an array of integers. The recursive algorithm should be in-place (i.e., its memory requirements should not grow with the input size). You will also implement an iterative version of the algorithm to find the minimum integer in an array of integers and compare the performance of the recursive vs. iterative versions.
Run the implementations for 100 times (automated via a loop) for each of the following values of array size and average the results of the run-time (in milliseconds or nanoseconds, if the former is a very small value) for the recursive and iterative versions. Array Size: 10, 102 , 103 , 104 , 105.
The value of the elements in the array can range from 0 to 9999.
Submit:
CODE Should Be In Java
(a - 15 pts) Pseudo code for the recursive algorithm and the explanation of its working
(b - 8 pts) Justify the space-complexity of the algorithm is (1), i.e., it is in-place.
(c - 12 pts) Analyze the time-complexity of the algorithm and show that it is (n) for an array of size 'n'
(d - 45 pts) Code for the implementations of the recursive (30 pts) and iterative algorithms (15 pts)
(e - 10 pts) Excel chart comparing the average run-time of the recursive vs. iterative algorithms for the different array sizes (consider plotting the X and Y values on a logarithmic scale, to the base 10)
(f - 10 pts) Your interpretation of the results for the average run-time of the iterative and recursive versions.
Explanation / Answer
JAVA PROGRAMME :
def min_num_iterative(arr):
k=arr[0]
for i in arr:
if i<k:
k=i
return k
def min_num_recursive(arr,low,high,min_num):
if low>high-1:
return min_num
else:
if arr[low]<=min_num:
min_num=arr[low]
low=low+1
return min_num_recursive(arr, low, high, min_num)
arr=[2,4,5,10,1,30,20,12,30,11]
print 'Minimum number using iteration=%d' %min_num_iterative(arr)
min=arr[0]
print 'Minimum number using recursion=%d' %min_num_recursive(arr,0,len(arr),min)
OUTPUT :
Minimum number using iteration=1
Minimum number using recursion=1
Space Complexity Justification: Here we use only one variable so, the space compexity is o(1)
Time complexity is o
(n)
because for any input n we go through the loop or recursion only once so time complexity is o
(n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.