Algorithms and theory for clustering and nonconvex quadratic programming