It’s well-known that a Muslim should cover his `Awrah (parts of his body which should not be exposed in front of others). By doing so, a Muslim preserves his dignity, morality, honor, etc.
With regards to wearing a shorts in sports, Sheikh Ahmad Kutty, an Islamic scholar said:
“There is nothing wrong in wearing shorts for men according to the consensus of scholars if the shorts are long enough to cover from the navel to the knee. However, if they are too short to cover up to the knee, then it is controversial among scholars. Therefore, it is not desirable for a Muslim to expose what is above his knee in public. However, we cannot say it is strictly Haram (prohibited) if a little bit above knee is exposed if his private parts are fully covered from both the front and the rear. The reason for this is that there are certain Hadiths that indicate such permissibility when people are interacting with close friends. Therefore, it is considered undesirable to do so in public without discrimination.
In case of swimming costumes, it is important to use costumes that cover from navel to knee as much as possible. However, it may be excused, in case of necessity, if little bit of navel is exposed, while the private parts are fully covered from both sides. Allah says: “Fear Allah as much as you can.” (Qur’an 64 Suratu Taghabun, verse: 16)