mirror of
https://github.com/supleed2/ELEC70056-HSV-CW1.git
synced 2024-12-22 21:55:48 +00:00
Add verfication for partition
This commit is contained in:
parent
88e407c8f7
commit
b97546132c
|
@ -62,7 +62,7 @@ method binarysearch_iter(A:array<int>, v:int) returns (result:bool)
|
||||||
|
|
||||||
method partition(A:array<int>, lo:int, hi:int) returns (pivot:int)
|
method partition(A:array<int>, lo:int, hi:int) returns (pivot:int)
|
||||||
requires 0 <= lo < hi <= A.Length
|
requires 0 <= lo < hi <= A.Length
|
||||||
ensures 0 <= lo <= pivot < hi
|
ensures 0 <= lo <= pivot < hi <= A.Length
|
||||||
ensures forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k]
|
ensures forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k]
|
||||||
modifies A
|
modifies A
|
||||||
{
|
{
|
||||||
|
@ -71,6 +71,9 @@ method partition(A:array<int>, lo:int, hi:int) returns (pivot:int)
|
||||||
while i < hi
|
while i < hi
|
||||||
invariant 0 <= lo <= pivot < i <= hi
|
invariant 0 <= lo <= pivot < i <= hi
|
||||||
invariant forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k]
|
invariant forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k]
|
||||||
|
invariant forall k :: (k==pivot) ==> A[k] == old(A[lo])
|
||||||
|
invariant forall k :: (lo <= k <= pivot) ==> A[k] <= A[pivot]
|
||||||
|
invariant forall k :: (pivot <= k < i) ==> A[k] >= A[pivot]
|
||||||
decreases hi - i
|
decreases hi - i
|
||||||
{
|
{
|
||||||
if A[i] < A[pivot] {
|
if A[i] < A[pivot] {
|
||||||
|
@ -79,6 +82,10 @@ method partition(A:array<int>, lo:int, hi:int) returns (pivot:int)
|
||||||
A[i] := A[j];
|
A[i] := A[j];
|
||||||
while pivot < j
|
while pivot < j
|
||||||
invariant forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k]
|
invariant forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k]
|
||||||
|
invariant forall k :: (k==pivot) ==> A[k] == old(A[lo])
|
||||||
|
invariant forall k :: (pivot <= k <= i) ==> A[k] >= A[pivot]
|
||||||
|
invariant forall k :: (lo <= k <= pivot) ==> A[k] <= A[pivot]
|
||||||
|
invariant A[pivot] > tmp
|
||||||
decreases j
|
decreases j
|
||||||
{
|
{
|
||||||
A[j+1] := A[j];
|
A[j+1] := A[j];
|
||||||
|
@ -106,6 +113,7 @@ method quicksort_between(A:array<int>, lo:int, hi:int)
|
||||||
|
|
||||||
method quicksort(A:array<int>)
|
method quicksort(A:array<int>)
|
||||||
modifies A
|
modifies A
|
||||||
|
ensures sorted(A)
|
||||||
{
|
{
|
||||||
quicksort_between(A, 0, A.Length);
|
quicksort_between(A, 0, A.Length);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue