Changed array.sort to insert a sorted element optimized