The following O(n^2) sorting algorithm works but I can't figure out why.
array a // length n, indexed 0 to n-1
for ( i = 0; i < n; i++)
for ( j = 0; j < n; j++)
if(a[i]<=a[j])
swap(a[i],a[j]);
It seems it is not bubble sort as after a single iteration neither the minimum or maximum is in place. (And also it needs n^2 comparisons instead of n*(n-1)/2)
How would you prove this algorithm sorts ? How is this sort algorithm called?
An execution of the sort for the example :
initial target
194600925
592040703
20865352
814014281
792862803
target after iteration 0
814014281
194600925
20865352
592040703
792862803
target after iteration 1
194600925
814014281
20865352
592040703
792862803
target after iteration 2
20865352
194600925
814014281
592040703
792862803
target after iteration 3
20865352
194600925
592040703
814014281
792862803
target after iteration 4
20865352
194600925
592040703
792862803
814014281