Both algorithms are O(n^2). The second one is always twice as fast, though.