Use another algorithm, because yours has O(n^2).