Praise be to Allah.
What you have mentioned about widespread temptation because of free mixing is something shameful. How often has free mixing between the sexes (men and women) in educational establishments, workplaces or elsewhere, been the cause of temptation and widespread evil! The one who never spoke on the basis of his own whims and desires (i.e., the Prophet, peace and blessings of Allah be upon him) spoke the truth when he said: “I have not left behind me any fitnah (trial) that is more harmful to men than woman.” Narrated by al-Bukhaari (5096) and Muslim (2740).
For more information, please see the answer to question no. 33651
You should keep far away from fitnah (trial or temptation). If you stay in this mixed school, there is the fear that this may lead to bad consequences, for the one who grazes his flocks around a sanctuary will soon transgress upon it.
You have to look for a school where you will be able to find what you want, without exposing yourself and your religious commitment to danger and things that may lead to your doom, even if that means that you have to study without attending the school (e. g., distance learning, online or otherwise).
If that is not possible, and no school in your country is free of mixing, then you must adhere to the Islamic teachings of lowering the gaze, and not getting carried away when talking to women. May Allah help us and you to obey Him, and help us to avoid temptation both visible and hidden.
For more information, please see the answer to question no. 127946
Ghusl does not become obligatory just because of seeing a non-mahram woman or speaking to her, so long as no maniy (semen) is emitted as a result of that. If maniy is emitted, then ghusl becomes obligatory. But if madhiy (prostatic fluid) is emitted because of thinking about desires or because of looking, then in this case it is obligatory to wash the penis and testicles, and to pour water over whatever has gotten onto one’s clothes.
The Muslim must strive to control himself and make himself lower his gaze as much as he can. If his gaze happens to fall on a woman who is not permissible for him, without him intending to do that, then this is forgiven, but he is instructed to avert his gaze after that. This has been explained in the answer to question no. 1774
What you must do is lower your gaze and block those bad thoughts that come to you; do not give them free rein. Blocking them is the best way of getting rid of them. For more information, please see question no. 20161.
And Allah knows best.