Select the algorithm pseudo-code:
For j=1-n-1 smallest = j for i=j+1 to N if A[i]<a[smallest] smallest=i a[j]=a[smallest] cyclic invariant at f Each iteration of the or loop (the loop variable is J) starts, and the sub-array containing the element a[1..j-1] pairs is ordered (consisting of the j-1 smallest element of the array [1..N]) and the condition of the loop termination is j>n-1, because the iteration J of each loop increases by 1, so there must be j=n. The j=n is brought into the loop invariant representation, and the elements in the Subarray a[1..n-1] are ordered (consisting of the n-1 smallest elements of the array a[1..n), so A[n] is the largest element. So the entire array has been sequenced. Best run and worst-case scenario (n^2)
This article is from the "Silly Endless Upgrade Tour" blog, make sure to keep this source http://zhangshifan.blog.51cto.com/9970564/1618381
Introduction to Algorithms third Edition exercise 2.2-2