Skip to content
Snippets Groups Projects
Commit 80c3df73 authored by Régis Blanc's avatar Régis Blanc
Browse files

remove invariants of BubbleSort to identify a clearer bug

parent 67e30baf
No related branches found
No related tags found
No related merge requests found
...@@ -18,23 +18,9 @@ object BubbleSortBug { ...@@ -18,23 +18,9 @@ object BubbleSortBug {
sa(j+1) = tmp sa(j+1) = tmp
} }
j = j + 1 j = j + 1
}) invariant( }) invariant(j >= 0 && j <= i && i < sa.length)
j >= 0 &&
j <= i &&
i < sa.length &&
sa.length >= 0 &&
partitioned(sa, 0, i, i+1, sa.length-1) &&
sorted(sa, i, sa.length-1) &&
partitioned(sa, 0, j, j+1, j+1)
)
i = i - 1 i = i - 1
}) invariant( }) invariant(i >= 0 && i < sa.length)
i >= 0 &&
i < sa.length &&
sa.length >= 0 &&
partitioned(sa, 0, i, i+1, sa.length-1) &&
sorted(sa, i, sa.length-1)
)
sa sa
}) ensuring(res => sorted(res, 0, a.length-1)) }) ensuring(res => sorted(res, 0, a.length-1))
...@@ -50,25 +36,4 @@ object BubbleSortBug { ...@@ -50,25 +36,4 @@ object BubbleSortBug {
isSorted isSorted
} }
def partitioned(a: Array[Int], l1: Int, u1: Int, l2: Int, u2: Int): Boolean = {
require(a.length >= 0 && l1 >= 0 && u1 < l2 && u2 < a.length)
if(l2 > u2 || l1 > u1)
true
else {
var i = l1
var j = l2
var isPartitionned = true
(while(i <= u1) {
j = l2
(while(j <= u2) {
if(a(i) > a(j))
isPartitionned = false
j = j + 1
}) invariant(j >= l2 && i <= u1)
i = i + 1
}) invariant(i >= l1)
isPartitionned
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment