Merge sort vs bubble sort when data is nearly sorted.
Asked in the Technical stage. Implement an in-place sorting function for an integer array that is expected to be nearly sorted. Use bubble sort with an early-termination optimization, then explain its complexity and when merge sort would be a better choice.
sort_nearly_sorted(nums) receives a mutable list of integers and returns the same list after sorting it in ascending order.
def sort_nearly_sorted(nums):