improve reverse array algorithm