Project 4: Recursive Algorithm to Find the Minimum Integer in an Array and its S
ID: 3723522 • Letter: P
Question
Project 4: Recursive Algorithm to Find the Minimum Integer in an Array and its Space-
Time Tradeoff Performance Comparison Analysis with an Iterative Algorithm
Due: March 8th, by 1 PM
Design and implement a recursive algorithm using Java 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, 10^2, 10^3, 10^4, 10^5
The value of the elements in the array can range from 0 to 9999.
Explanation / Answer
Program :-
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)
executed 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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.