Another optimization is to terminate if no swaps occur during a pass.
The more efficient bidirectional variant of this algorithm, which reverses the direction after each pass, is referred to by the term "cocktail shaker". Another optimization is to terminate if no swaps occur during a pass. It's not insertion sort, but for n items it approaches a time complexity of Big O of n for mostly sorted data. However, it has a quadratic runtime in the worst case and is less efficient than insertion and selection sort in practice. This algorithm works by comparing adjacent items and swapping them if they are in the wrong order. For 10 points name this sorting algorithm which is named for the way that elements move up the list.
The more efficient bidirectional variant of this algorithm, which reverses the direction after each pass, is referred to by the term "cocktail shaker".
bubble sort [accept: sinking sort]