To apply for political asylum you have to be physically present in the US. You can, however, apply for refugee status (which is the same as political asylum) while outside of the US. For more information on applying for refugee status, you should review the instructions provided on the USCIS web site at:
http://uscis.gov/graphics/howdoi/refapp.htm
When applying for refugee (or asylum) status, you’ll have to show that you face persecution in Algeria because of your sexual orientation. Simply being gay will not necessarily qualify you for refugee status.