TITLE: Orthogonal Projection onto the Unit Simplex
QUESTION [8 upvotes]: The Unit Simplex is defined by:
$$ \mathcal{S} = \left\{ x \in \mathbb{{R}^{n}} \mid x \succeq 0, \, \boldsymbol{1}^{T} x = 1 \right\} $$
Orthogonal Projection onto the Unit Simplex is defined by:
$$
\begin{alignat*}{3}
\arg \min_{x} & \quad & \frac{1}{2} \left\| x - y \right\|_{2}^{2} \\
\text{subject to} & \quad & x \succeq 0 \\
& \quad & \boldsymbol{1}^{T} x = 1
\end{alignat*}
$$ 
How could one solve this convex optimization problem?

REPLY [3 votes]: The paper by Condat [1] presents a review and comparison of existing algorithms with a new proposal for projection onto the unit simplex. This paper lists the worst-case complexity and empirical complexity of those algorithms, and presents concise pseudo-code for all algorithms. In particular, the algorithm proposed by Condat takes $O(n)$ time in practice, whereas sorting-based methods take $O(n \log n)$ time in practice. I have implemented Condat's algorithm in the past, and can vouch for its speed relative to direct sorting-based approaches.
[1] Laurent Condat, Fast Projection onto the Simplex and the $\ell_1$ Ball.
EDIT: Condat has included C and MATLAB implementations of all the algorithms mentioned in his paper here: https://lcondat.github.io/software.html