Please help reducing the time complexity of the code

This is the problem. Please help in reducing the time complexity of my code (sorting problem with n<=10^6).
my code is taking nlogn. And look if theres any other possible mistake in the code. Following is my code with some comments you’ll find helpful. Thanks!

Hey @niketagarwal10
Please properly indent your code and also add 1 liner comments above each loop ,what u are trying to do in it

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.