Bubble sort is a stable, in-place sort with a best, worst and average case of O(n!) for n elements, thus making it highly inefficient and entirely unsuitable for sorting large amounts of data. For this reason bubblesort is often cited as an example of how not to write an algorithm.
The algorithm starts by accepting a zero-based array with n elements (0 to n-1). While n is greater than 1, the algorithm iterates an outer loop. On each iteration of the outer loop, an inner loop traverses from element index 1 to n-1. On each iteration of the inner loop, the element at the current index is compared with the element at the previous index. If they are out of order, the two elements are swapped. When the inner loop has finished, n is decremented. At this point element n is now its correct place and is ignored on the next iteration of the outer loop. When n is not greater than 1, all the elements are sorted and the outer loop terminates.
In other words, the algorithm locates the largest value in the range 0 to n-1 and places it at index n-1 (bubbling it up the list with each swap). After decrementing n, everything from element n up is sorted and everything below n is unsorted, thus creating two subarrays split at n. On each pass, the sorted subarray gains a new element and the unsorted subarray loses an element. When there is only one element in the unsorted subarray, the entire array is sorted.
The inefficiency of the algorithm is that it takes no account of elements at the end of the unsorted subarray that may already be in their correct position. The algorithm can be improved with the observation that the position of the last swap at the end of the inner loop means that everything from that point forwards is already sorted and don't need to be compared on the next pass. So rather than reducing the unsorted subarray by just one element, the subarray can be reduce by one or more elements. To achieve this we initialise a temporary variable with the value zero at the start of the outer loop, and whenever a swap occurs in the inner loop we assign the current index to the temporary variable. When the inner loop ends, the temporary variable indicates where the last swap occured and we can assign that value to n, which may reduce n by one or more elements on each pass. The only other change we need to make is that the outer loop now terminates when n is zero (the initial value of the temporary variable), which means no swaps occured on the last iteration of the inner loop, so everything must be sorted.
Even with this optimisation the worst case is still O(n!) if the list is completely reversed. However, for an already-sorted list the best case becomes O(n). Since these two extremes are isolated cases, the average case is somewhere in between, around O(n!/2), which is still highly inefficient and unsuitable for large amounts of data.
The following is an implemention of an optimal bubble sort in C++:
template<typename _Ty>
void bubble (std::vector<_Ty>& A)
{
unsigned size = A.size();
while (size)
{
unsigned temp = 0; // records last swap position
for (unsigned index=1; index<size; ++index)
{
if (A[index]<A[index-1])
{
std::swap (A[index],A[index-1]);
temp=index;
}
}
size = temp;
}
}
Bubble sort is a highly inefficient sorting algorithm, often cited as an example of how not to write an algorithm. Bubble sort is trivial to implement but has O(n*n) complexity when sorting n items. While perfectly adequate for small data sets, it is wholly inappropriate for large sets. Nevertheless, insert sort is, on average, more efficient for sorting small sets.
The bubble sort algorithm can be applied to an array of characters. Every character can be translated to an integer equivalent via the ascii table
This is false. The movement described is a disadvantageof bubble sort.
n-1 times
Bubble sort got its name because if you could watch the way your data was changing, on each iteration you would see the greatest number "bubble" to the top.Similarly, you could said that you would see the lowest number "sink" to the bottom.
Use a sorting algorithm. There are a bewildering number of sorting algorithms, both stable and unstable. To sort numbers, an unstable sort suffices. The algorithm you use will depend on how many numbers need to be sorted (a small or a large set), however a hybrid algorithm (a combination of two or more algorithms) can cater for both. Introsort (unstable) and timsort (stable) are the two most common hybrid sorting algorithms.
The bubble sort algorithm can be applied to an array of characters. Every character can be translated to an integer equivalent via the ascii table
Bubble sort is an "in place" algorithm. Other than a temporary "switch" variable, no extra space is required.
Bubble sort has no practical applications other than that it is often cited as an example of how not to write an algorithm. Insert sort is the best algorithm for sorting small lists of items and is often used in conjunction with quick sort to sort larger lists. Like insert sort, bubble sort is simple to implement and is a stable sort (equal items remain in the same order they were input). However, insert sort uses copy or move operations rather than swaps (which is actually three operations per swap) and is therefore quicker. The only time a bubble sort will work quicker than insert sort is when the array is already sorted, which renders the entire algorithm redundant. A modified algorithm that specifically tests if an array is sorted or not would be more efficient than a single-pass bubble sort.
This is false. The movement described is a disadvantageof bubble sort.
n-1 times
Bubble sort got its name because if you could watch the way your data was changing, on each iteration you would see the greatest number "bubble" to the top.Similarly, you could said that you would see the lowest number "sink" to the bottom.
Use a sorting algorithm. There are a bewildering number of sorting algorithms, both stable and unstable. To sort numbers, an unstable sort suffices. The algorithm you use will depend on how many numbers need to be sorted (a small or a large set), however a hybrid algorithm (a combination of two or more algorithms) can cater for both. Introsort (unstable) and timsort (stable) are the two most common hybrid sorting algorithms.
Bubble sort, incorrectly referred to as sinking sort is a simple sorting algorithm (step by step procedure of calculations) that works to repeatedly step through list to be sorted. With the comparing of each adjacent items and swapping them into the correct order. This repeated until there are no swaps needed and the list of items are sorted.
Bubble sort is also known as sinking sort.
A bubble sort may have a range from O(n-1) for a pre-sorted array, to O(n2-n) for a poorly implemented bubble sort algorithm. Given 20 elements, a best case scenario is 19 comparisons, and the worst case is 380 comparisons.
Time complexity Best case: The best case complexity of bubble sort is O(n). When sorting is not required, all the elements are already sorted. Average case: The average case complexity of bubble sort is O(n*n). It occurs when the elements are jumbled, neither properly ascending nor descending. Worst case: The worst-case complexity of bubble sort is O(n*n). It occurs when the array elements are needed to be sorted in reverse order. Space complexity In the bubble sort algorithm, space complexity is O(1) as an extra variable is needed for swapping.
Bubble sort will be able to sort the items lexicographically, if you first assign the strings a relative value. The way strcmp in the C library does this is by returning the difference of the first different characters between the strings. For instance, if you call strcmp("Hello", "Highway"), strcmp will return 'e'-'i'. The ascii value of e on a unix system is 101 and the ascii value of i is 105, so it is returning the value 101-105 = -4 which will tell you that "Hello" is lexicographically smaller than "Highway". Doing this, you should be able to use any algorithm you want to sort the strings.