Skip to content

Commit 8e1491a

Browse files
author
davidcok
committed
Fixed SelectionSort
1 parent 20c87ee commit 8e1491a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

examples/SelectionSort.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@ public static void sort(int /*@ non_null @*/ [] arr) {
1616
//@ loop_assigns i, arr[*];
1717
//@ decreasing n-i; // i goes up
1818
for (int i = 0; i < arr.length; i++) {
19+
// Sorted sublist is from 0 up to but not including i
20+
21+
// Find maximum in i .. end
1922
int ub = arr[i];
2023
int l = i;
2124

2225
//@ loop_invariant i < j <= n; // Bounds check
2326
//@ loop_invariant \forall int k; i <= k < j; ub >= arr[k]; // max elem up-to j
2427
//@ loop_invariant i <= l < n; // max index bounds check
2528
//@ loop_invariant ub == arr[l]; // max index corresponds to max elem.
26-
//@ loop_assigns j, arr[*];
29+
//@ loop_assigns j, l, ub;
2730
//@ decreasing n-j; // j goes up
2831
for (int j = i+1; j < arr.length; j++) {
2932
if (arr[j] > ub) {
3033
l = j;
3134
ub = arr[j];
3235
}
3336
}
37+
// Maximum is at position l
38+
3439
// assert \forall int k; 0 <= k < i; largest <= arr[k];
3540
// assert \forall int k; i <= k < n; largest >= arr[k];
3641

0 commit comments

Comments
 (0)