\documentclass{article}
\usepackage{algorithm}
\begin{document}
\setcounter{section}{54}
\section{A section with no label}
\begin{algorithm}
\label{myalg}
\caption{An algorithm with the label `myalg'.}
\end{algorithm}
Reference to myalg: \ref{myalg}.
\end{document}
With this code, the reference givens 55 (the section number); if I move the label after the caption command, I get the correct reference (1).
Why should this be the case? I find it confusing: when thinking about an algorithm as an environment, you expect to be able to label it right after you began it. At the very least I would expect some kind of error or "??" to appear, rather than the previous environment or reference-able entity being used.
figureenvironment really has multiple figures. So afigureis not a figure, and you reference the caption instead of referencing the figure... also, in analgorithmenvironment you don't have that; I mean, maybe I can somehow have twoalgorithmics in there, but I would only reference the entire algorithm... – einpoklum Oct 22 '11 at 08:57algorithmenvironment doesn't generate a number; it's the\captionthat does it: maybe you won't, but someone else might want to keep together twoalgorithmicenvironments (each one with its caption), to show the differences between them, for instance. – egreg Oct 22 '11 at 09:03