Use the stdlib version of swap(a,b)
Use min/max from stdlib