Daniel Sutantyo, Department of Computing, Macquarie University
2.4 - Correctness of Iterative Algorithms
2.4 - Correctness of Iterative Algorithms
2.4 - Correctness of Iterative Algorithms
void selection_sort(int arr[]){
for (int i = 0; i < n-1; i++){
int min_index = i;
for (int j = i+1; j < n; j++){
if (arr[j] < arr[min_index])
min_index = j;
}
swap(i,min_index);
}
}
2.4 - Correctness of Iterative Algorithms
2.4 - Correctness of Iterative Algorithms
2.4 - Correctness of Iterative Algorithms
void selection_sort(int arr[]){
for (int i = 0; i < n-1; i++){
int min_index = i;
for (int j = i+1; j < n; j++){
if (arr[j] < arr[min_index])
min_index = j;
}
swap(i,min_index);
}
}
2.4 - Correctness of Iterative Algorithms
void selection_sort(int arr[]){
for (int i = 0; i < n-1; i++){
// find the index of the smallest element in arr[i .. n-1]
int min_index = find_minimum(arr,i);
swap(i,min_index);
}
}
2.4 - Correctness of Iterative Algorithms
void selection_sort(int arr[]){
for (int i = 0; i < n-1; i++){
// find the index of the smallest element in arr[i .. n-1]
int min_index = find_minimum(arr,i);
swap(i,min_index);
}
}
2
3
5
7
11
8
2
3
5
7
8
11
the last element is always the largest one!
2.4 - Correctness of Iterative Algorithms
void selection_sort(int arr[]){
for (int i = 0; i < n-1; i++){
// find the index of the smallest element in arr[i .. n-1]
int min_index = find_minimum(arr,i);
swap(i,min_index);
}
}
2.4 - Correctness of Iterative Algorithms
The subarray arr[0 .. i-1] is in sorted order
AND
all the values in arr[0 .. i-1] are smaller than or equal to the values in arr[i..n-1]
2.4 - Correctness of Iterative Algorithms
The subarray arr[0 .. i-1] is in sorted order AND all the values in arr[0 .. i-1] are smaller than or equal to the values in arr[i..n-1]
2.4 - Correctness of Iterative Algorithms
The subarray arr[0 .. i-1] is in sorted order AND all the values in arr[0 .. i-1] are smaller than or equal to the values in arr[i..n-1]
2.4 - Correctness of Iterative Algorithms
horner(x,i):
if (i == n)
return c_n
else
return c_i + x * evaluate(x,i+1)
\[ f(x) = c_nx^n + c_{n-1}x^{n-1} + \cdots +c_1x + c_0 \]
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n; i >= 0; i--)
y = c[i] + x * y;
return y
2.4 - Correctness of Iterative Algorithms
\[ f(x) = c_nx^n + c_{n-1}x^{n-1} + \cdots +c_1x + c_0 \]
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n; i >= 0; i--)
y = c[i] + x * y;
return y
\[\text{horner}(x,i) = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
\[\text{horner}(x,0) = c_nx^{n} + c_{n-1}x^{n-1} + \cdots + c_{1}x + c_0\]
\[\text{horner}(x,n) = c_nx^{n}\]
2.4 - Correctness of Iterative Algorithms
\[\text{horner}(x,i) = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
\(i = n\)
\[c_nx^{n-n} + c_{n-1}x^{n-1-n} + \cdots + c_{n+1}x + c_n\]
\(i = n-1\)
\[c_nx^{n-n+1} + c_{n-1}x^{n-1-n+1} + \cdots + c_{n}x + c_{n-1}\]
2.4 - Correctness of Iterative Algorithms
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n; i >= 0; i--)
y = c[i] + x * y;
return y
\[y = c_n\]
but, it's not ... at the start of the loop, \(y = 0\)
2.4 - Correctness of Iterative Algorithms
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n; i >= 0; i--)
y = c[i] + x * y;
return y
\[y = c_n\]
but, it's not ... at the start of the loop, \(y = 0\)
2.4 - Correctness of Iterative Algorithms
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n; i >= 0; i--)
y = c[i] + x * y;
return y
\[y = c_nx^{n+1} + c_{n-1}x^{n} + \cdots + c_{0}x + c_{-1}\]
2.4 - Correctness of Iterative Algorithms
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n; i >= 0; i--)
y = c[i] + x * y;
return y
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n+1; i > 0; i--)
y = c[i-1] + x * y;
return y
2.4 - Correctness of Iterative Algorithms
\[y = 0\]
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n+1; i > 0; i--)
y = c[i-1] + x * y;
return y
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
2.4 - Correctness of Iterative Algorithms
\[y = c_nx^{n-k} + c_{n-1}x^{n-1-k} + \cdots + c_{k+1}x + c_k\]
then we multiply \(y\) by \(x\) and add \(c_{k-1}\)
\[y = c_nx^{n-k+1} + c_{n-1}x^{n-k} + \cdots + c_{k+1}x + c_kx + c_{k-1}\]
which is
\[y = c_nx^{n-(k-1)} + c_{n-1}x^{n-1-(k-1)} + \cdots + c_{k}x + c_{k-1}\]
this is just the loop invariant with \(i = k-1\)
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
so it is preserved
2.4 - Correctness of Iterative Algorithms
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n+1; i > 0; i--)
y = c[i-1] + x * y;
return y
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
\[y = c_nx^{n} + c_{n-1}x^{n-1} + \cdots + c_{1}x + c_0\]
2.4 - Correctness of Iterative Algorithms
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n+1; i > 0; i--)
y = c[i-1] + x * y;
return y
\[y = c_nx^{n-i} + c_{n-1}x^{n-1-i} + \cdots + c_{i+1}x + c_i\]
\[y = c_nx^{n-(i+1)} + c_{n-1}x^{n-1-(i+1)} + \cdots + c_{i+2}x + c_{i+1}\]
L2 :
L1 :
// assuming the coefficients are in c[0..n]
int horner(int x, int[] c):
int y = 0, n = c.length-1;
for (int i = n; i >= 0; i--)
y = c[i] + x * y;
return y