-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_insertion_sort.c
More file actions
34 lines (30 loc) · 822 Bytes
/
Copy path13_insertion_sort.c
File metadata and controls
34 lines (30 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "sorting.h"
/*@ requires \valid(t + (0..n-1));
ensures Sorted(t, 0, n-1);
*/
void insert_sort(int t[], int n)
{
int i,j;
int mv;
if (n <= 1) return;
/*@ loop invariant 0 <= i <= n;
loop invariant Sorted(t, 0, i);
loop variant n - i;
*/
for (i = 1; i < n; i++) {
// assuming t[0..i-1] is sorted, insert t[i] at the right place
mv = t[i];
// look for the right index j to put t[i]
/*@ loop invariant 0 <= j <= i;
loop invariant j == i ==> Sorted(t, 0, i);
loop invariant j < i ==> Sorted(t, 0, i+1);
loop invariant \forall integer k; j <= k < i ==> t[k] > mv;
loop variant j;
*/
for (j = i; j > 0; j--) {
if (t[j-1] <= mv) break;
t[j] = t[j-1];
}
t[j] = mv;
}
}