def pancake_sort(nums):
    n = len(nums)
    for size in range(n, 1, -1):
        # Find the index of the maximum element in nums[0:size]
        max_index = nums.index(max(nums[:size]))
        
        if max_index == size - 1:
            continue
        
        # Flip to bring max to front if not already there
        if max_index != 0:
            nums[:max_index + 1] = nums[:max_index + 1][::-1]
        
        # Flip to move max to its correct position
        nums[:size] = nums[:size][::-1]
    
    return nums