Coctail sort refactored