Your algorithm has a typo. The inner loop should start $j := i + 1$ and go to $n$. Notice that after iteration $i$ of the outer loop, the largest $i$ elements are sorted correctly at the end of the list. Prove this fact, which I will call $(*)$.
We also have termination, by finiteness of both loops (and no modification of $i, j$ other than incrementation).
You can then use induction on $(*)$ to argue that if the largest elements are sorted correctly at indices $n-i, ..., n-1$, then the next iteration of the loop will correctly place the $n-1-i$ largest element at index $n - 1 - i$, preserving order.
Edit:
Claim: On the ith iteration of the outer loop, the largest $i$ elements will be sorted correctly (at the end of the array).
Proof: By induction on $n \in \mathbb{N}$. Consider the base case of $n = 1$. Let $x$ be the largest element in the array. By the algorithm, if $x$ is unique, $x$ is swapped on each iteration after being discovered initially. It is then placed at the end. If $x$ is not unique, then there exists a second copy of it and no swap will occur. However, the second copy will then be swapped. This process continues, so the last occurrence of $x$ will be moved to the end of the array. The theorem thus holds at $i = 1$.
I assume the theorem holds true up to an arbitrary integer $k$ such that $1 < k < n$ and prove true for the $k+1$ case. Let $y$ be the $k+1$ largest element in the array. We consider the reduced case of examining the first $n - k - 1$ elements. By the inductive hypothesis, $y$ will be correctly placed at the end of this sub-array. As $y$ is no bigger than the $k$th largest element, $y$ will not be moved further down into the array. The $k+1$ case thus holds.
And so the claim holds true by the principle of mathematical induction.
The steps are given in reverse order. The $(3,1,1)$ should be $(3,1,0)$.
You can see what is going on by using print()
function before returning.
def extended_euclid(a,b):
if b == 0:
print([a,b],[a,1,0]);
return (a,1,0)
else:
(d_1,x_1,y_1) = extended_euclid(b, a % b)
(d,x,y) = (d_1,y_1,x_1 - (a//b) * y_1)
print([a,b],[d,x,y]);
return (d,x,y)
If you execute extended_euclid(99,78)
then the resulting output is
[3, 0] [3, 1, 0]
[6, 3] [3, 0, 1]
[15, 6] [3, 1, -2]
[21, 15] [3, -2, 3]
[78, 21] [3, 3, -11]
[99, 78] [3, -11, 14]
while the function returns [3,-11,14]
. This means that
$$ 3 = (-11)\cdot 99 + (14)\cdot 78 = -1089 + 1092 $$
is the gcd $\,3\,$ expressed as a linear combination
of the two numbers $\,99,78.$
Best Answer
Sorry if I am misreading, but it seems like your question is partly about induction itself, so let me give an overview.
Suppose you want to climb up a ladder. To do this, it suffices to know:
If $P(n)$ is the statement "you can reach the $n$-th step," then we can rewrite this as
To be clear, this is nothing fancy, and it's entirely obvious once you understand it. Since $P(1)$ implies $P(2)$ and $P(1)$ is true, $P(2)$ is true. Since $P(2)\implies P(3)$, $P(3)$ is true, etc.
In your case, $P(k)$ is the statement "$\operatorname{square}(k)=k^2$."