


Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. This has led to these types of elements being named rabbits and turtles, respectively, after the characters in Aesop's fable of The Tortoise and the Hare. If the smallest element is at the end of the list, it will take n−1 passes to move it to the beginning. On the other hand, an element that must move toward the beginning of the list cannot move faster than one step per pass, so elements move toward the beginning very slowly. For example, the largest element in the list will win every swap, so it moves to its sorted position on the first pass even if it starts near the beginning. An element that must move toward the end of the list can move quickly because it can take part in successive swaps. The distance and direction that elements must move during the sort determine bubble sort's performance because elements move in different directions at different speeds. Additionally, if this behavior is desired, it can be trivially added to any other algorithm by checking the list before the algorithm runs. However, not only does insertion sort share this advantage, but it also performs better on a list that is substantially sorted (having a small number of inversions). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the set and thus are more complex. When the list is already sorted (best-case), the complexity of bubble sort is only O( n). The only significant advantage that bubble sort has over most other algorithms, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted efficiently is built into the algorithm. Therefore, bubble sort is not a practical sorting algorithm. Even other О( n 2) sorting algorithms, such as insertion sort, generally run faster than bubble sort, and are no more complex. Most practical sorting algorithms have substantially better worst-case or average complexity, often O( n log n). Performance īubble sort has a worst-case and average complexity of О( n 2), where n is the number of items being sorted. After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared. Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one). More efficient algorithms such as quicksort, timsort, or merge sort are used by the sorting libraries built into popular programming languages such as Python and Java. This simple algorithm performs poorly in real world use and is used primarily as an educational tool. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. The pass through the list is repeated until the list is sorted. O ( n 2 ) auxiliaryīubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
