-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrectnessInvariant.cpp
More file actions
38 lines (32 loc) · 1.02 KB
/
correctnessInvariant.cpp
File metadata and controls
38 lines (32 loc) · 1.02 KB
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
33
34
35
36
37
38
#include <iostream>
#include <vector>
using namespace std;
void insertionSort(int N, int arr[]) {
int i, j;
int value;
for (i = 1; i < N; i++) {
value = arr[i];
j = i - 1;
while (j >= 0 && value < arr[j]) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = value;
}
for (j = 0; j < N; j++) {
printf("%d", arr[j]);
printf(" ");
}
}
// Info: Testcases Worked
//int main() {
// int arrayToSort[] = {45,53,53,2,5,1,56,7,4,3,56,6,5,4,5,6,4};
// insertionSort(17, arrayToSort);
// return 0;
//}
/*
In computer science, you could prove it formally with a loop invariant, where you state that a desired property is maintained in your loop. Such a proof is broken down into the following parts:
Initialization: It is true (in a limited sense) before the loop runs.
Maintenance: If it's true before an iteration of a loop, it remains true before the next iteration.
Termination: It will terminate in a useful way once it is finished.
*/