简体   繁体   中英

Java sort method in JML

I need a sort method for JML I tried Insertion Sort but I don't know what requires and ensures or maintaingins I need. Please help. I need //@requires, //@ensures and //@maintaining.

public class InsertionSort

{

void sort(int arr[])
{
    int n = arr.length;
    for (int i=1; i<n; ++i)
    {
        int key = arr[i];
        int j = i-1;
        while (j>=0 && arr[j] > key)
        {
            arr[j+1] = arr[j];
            j = j-1;
        }
        arr[j+1] = key;
    }
}
}

The following ensures ascending order and preserves repetitions.

//@ assignable arr[*];
//@ requires arr != null;
//@ ensures (\forall int i; 0 <= i && i <= arr.length-1; arr[i] <= arr[i+1]) &&
//@         (\forall int i; 0 <= i && i <= arr.length;
//@            (\num_of int j; 0 <= j && j <= arr.length;
//@               arr[i] == \old(arr[j])) ==
//@            (\num_of int j; 0 <= j && j <= arr.length;
//@               arr[i] == arr[j])
//@         );
//@            
void sort(int arr[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM