diff --git a/BinarySearch/src/BinSearch.java b/BinarySearch/src/BinSearch.java index 07e208a..b36f919 100644 --- a/BinarySearch/src/BinSearch.java +++ b/BinarySearch/src/BinSearch.java @@ -6,13 +6,24 @@ class BinSearch { @ ensures 0 <= \result < a.length; @ ensures a[\result] == v; @ assignable \nothing; + @ also + @ private exceptional_behavior + @ requires !(\exists int idx; 0 <= idx < a.length; a[idx] == v); + @ assignable \nothing; + @ signals_only RuntimeException; @*/ private int binSearch(int[] a, int v) { int low = 0; int up = a.length; + /*@ loop_invariant 0 <= low <= up <= a.length; + @ loop_invariant (\forall int x; 0 <= x < low; a[x] != v); + @ loop_invariant (\forall int x; up <= x < a.length; a[x] != v); + @ assignable \nothing; + @ decreases up - low; + @*/ while (low < up) { - int mid = (low + up) / 2; + int mid = low + ((up - low) / 2); if (v == a[mid]) { return mid; } else if (v < a[mid]) {