« August 2021 »
August
MoTuWeThFrSaSu
1
2345678
9101112131415
16171819202122
23242526272829
3031
Uni-Logo
You are here: Home Teaching Summer Term 2017 Formal Methods for Java (Lecture) Resources Demos Lecture 14 LinearSearch.java
Document Actions

LinearSearch.java

LinearSearch.java — Java source code, 0Kb

File contents

public class LinearSearch {

    /*@ requires array != null;
      @ ensures 
      @   (\exists int j; 0 <= j && j < array.length; array[j] == elem) ?
      @     array[\result] == elem : \result == -1;
      @*/
    public int search(int[] array, int elem) {
        /*@ loop_invariant i >= 0;
          @ loop_invariant !(\exists int j; 0 <= j && j < i; array[j] == elem);
          @ decreases array.length - i;
          @*/
        for (int i = 0; i < array.length; i++) {
            if (array[i] == elem)
                return i;
        }
        return -1;
    }
}
Personal tools