需要一个带有引用的JML排序方法并确保

时间:2017-12-16 16:41:11

标签: java jml

我需要一个JML的排序方法我试过bubblesort但我不知道需要什么,并确保或维护我需要的东西。我是这种语言的新手。

public class BubbleSortExample { 
    //@ references
    static void bubbleSort(int[] arr)
    {
        int n = arr.length;
        int temp = 0;
        for(int i=0; i < n; i++)
        {
            for(int j=1; j < (n-i); j++)
            {
                if(arr[j-1] > arr[j])
                {
                    temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

KeY Project有一个BubbleSort的例子。您可以在KeY BookJML Reference的第二版中进一步了解有关JML的信息。

注意 JML的语法和语义可以通过不同的工具进行解释。

注意这个例子看起来有点旧,因为它不依赖于序列和置换理论。

// This file is part of KeY - Integrated Deductive Software Design 
//
// Copyright (C) 2001-2011 Universitaet Karlsruhe (TH), Germany 
//                         Universitaet Koblenz-Landau, Germany
//                         Chalmers University of Technology, Sweden
// Copyright (C) 2011-2013 Karlsruhe Institute of Technology, Germany 
//                         Technical University Darmstadt, Germany
//                         Chalmers University of Technology, Sweden
//
// The KeY system is protected by the GNU General 
// Public License. See LICENSE.TXT for details.
// 

public class BubbleSort{

    public int[] b;

    /*@ public normal_behavior
      @  requires a!=null && a.length>1 && a.length<5;
      @  requires a.length == b.length && 
      @           (\forall int i; 0<=i && i<a.length; b[i]==a[i]);
      @  ensures a!=null ==> (\forall int i; 0<=i && i<a.length-1; 
      @                        a[i]<=a[i+1]) &&
      @                      equalsModOrdering(a, b); 
      @*/
    public void sort(int[] a){
    if(a==null) return;
    boolean sorted = false;
    int help;
    while(!sorted){
        sorted=true;
        for(int i=0; i<a.length-1; i++){
        if(a[i]>a[i+1]){
            help = a[i];
            a[i] = a[i+1];
            a[i+1] = help;
            sorted = false;
        }
        }
    }   
    }

    public static /*@pure@*/ boolean equalsModOrdering(int[] a, int[] b){
    if(a.length!=b.length){
        return false;
    }
    int[] x = new int[a.length];
    int[] y = new int[a.length];
    copy(a, x);
    copy(b, y);
    return equalsModOrderingHelp(x, y, 0);
    }

    private static void copy(int[] a, int[] b){
    for(int i=0; i<a.length; i++){
        b[i] = a[i];
    }
    }

    private static boolean equalsModOrderingHelp(int[] a, int[] b, int start){
      if(start>=a.length) return true;
      int i = find(a[start], b, start);
      if(i==-1){
        return false;
      }
      b[i] = b[start];
      return equalsModOrderingHelp(a, b, start+1);
    }

    private static int find(int x, int[] ar, int i){
      while(i < ar.length){
        if(ar[i] == x){
        return i;
        }
        i++;
      }
      return -1;
    }

}