/* XXX possibly buggy! an example for testing. can you find all the bugs? */ /* Linear and binary search * 15-122 Principle of Imperative Computation, Spring 2011 * Frank Pfenning */ bool is_sorted(int[] A, int n) //@requires 0 <= n && n <= \length(A); { for (int i = 0; i < n-1; i++) //@loop_invariant n == 0 || (0 <= i && i <= n-1); { if (!(A[i] <= A[i+1])) return false; } return true; } bool is_in(int x, int[] A, int n) //@requires 0 <= n && n <= \length(A); { for (int i = 0; i < n; i++) //@loop_invariant 0 <= i && i <= n; if (A[i] == x) return true; return false; } int linsearch(int x, int[] A, int n) //@requires 0 <= n && n <= \length(A); //@requires is_sorted(A, n); /*@ensures (-1 == \result && !is_in(x, A, n)) || ((0 <= \result && \result < n) && A[\result] == x); @*/ { for (int i = 0; i < n; i++) //@loop_invariant 0 <= i && i <= n; //@loop_invariant i == 0 || A[i-1] < x; { if (A[i] == x) return i; else if (A[i] > x) return -1; } return -1; } int binsearch(int x, int[] A, int n) //@requires 0 <= n && n <= \length(A); //@requires is_sorted(A, n); /*@ensures (-1 == \result && !is_in(x, A, n)) || ((0 <= \result && \result < n) && A[\result] == x); @*/ { int lower = 0; int upper = n; while (lower < upper) //@loop_invariant 0 <= lower && lower <= upper && upper <= n; //@loop_invariant (lower == 0 || A[lower-1] < x); //@loop_invariant (upper == n || A[upper] > x); { int mid = lower + (upper-lower)/2; /* mid = (lower+upper)/2 could overflow! */ //@assert lower <= mid && mid < upper; if (A[mid] == x) return mid; else if (A[mid] > x) upper = mid+1; else //@assert A[mid] < x; lower = mid+1; } return -1; }