0
1 def recmin(A):
2 if len(A) == 1:
3   return A[0]
4 else:
5   m = len(A) // 2
6   min1 = recmin(A[0..m-1])
7   min2 = recmin(A[m..len(A)-1])
8   return min(min1, min2)

I'm trying to prove the partial correctness of this function, I figured out a predicate of p(i):for the array A=[0..i], len(A)=i+1 and when recmin[A]is called,then this call terminates and returns some t such that 0<=t<=i A[t] is the min value

But I feel like this predicate is wrong and how would i prove that the postcondition where A[0] is min value

ddd suman
  • 55
  • 1
  • 8
  • Not sure what language this is, but the result will be incorrect if `len(A) == 0` (i.e. an empty list). That may or may not be significant to you, though, as there may arguably not even be a correct answer in that case... – twalberg Jul 14 '15 at 20:16
  • I would try performing an induction over the length of the given list. When you recurse you know that the lengths of the sublists are shorter than the current input, so you can refer to the induction hypothesis to show that `recmin(A[0..m-1])` and `recmin(A[m..len(A)-1])` indeed gives the minimum element of the respective arrays. Showing that `min(min1, min2)` is indeed the global minimum can be done by referring to the definition of what the minimum element of a list is. – aioobe Jul 30 '15 at 14:39

0 Answers0