
As a result, bubble sort is not a useful sorting algorithm. Other O(n 2) sorting algorithms, such as insertion sort, are typically quicker than bubble sort and are not any more complex. O(n).ĭuring the first iteration of the outer loop, the inner loop executes n times without changing the flag, indicating that the data is sorted.įollowing table shows the time complexity of bubble sort for both versions, normal (without flag) and optimized (with flag): In this scenario, the best-case bubble sort running time would be linear, i.e. By setting an appropriate flag, we can break the cycle. It’s self-explanatory, that if no swapping occurs in the first run, the list is already sorted. As a result, the complexity of bubble sort is the same whether the situation is best, worst, or average. The quadratic complexity is caused by the two nested for loops, which iterate regardless of the input data pattern. Thus,ĭespite the fact that statements within the inner loop do not execute, the complexity of bubble sort is O(n 2).

Running time of algorithm is defined as total number of comparisons. In last iteration of outer loop, inner loop does only one comparison. For second iteration of outer loop, inner loop dose n – 1 comparison and so on. Optimized version of the algorithm is mentioned below: Algorithm OPTIMIZED_BUBBLE_SORT(A)įor j ← 1 to n – i do if A > A doįor first iteration of outer loop, inner loop does n comparisons. Pass – 6 Algorithm for Bubble Sort Algorithm BUBBLE_SORT(A)įor i ← 1 to n do for j ← 1 to n – i do if A > A doĪlthough the above logic would sort an unsorted array, the technique is inefficient since the outer for loop will continue to execute for n iterations even if the array is sorted. Adjacent cells in light gray colors are under comparison and dark gray cells indicate sorted elements. Step by step comparison and output of every pass is depicted in following figures. Let us sort the letters of word “ DESIGN” in alphabetical order using bubble sort. Following figures show the step by step simulation of it. In every iteration, the largest element from unsorted array moves to the last location. Example of Bubble Sortīubble sort compares adjacent elements and swaps them if they are out of order. Sorting libraries integrated into popular computer languages like as Python and Java utilize more efficient algorithms such as quicksort, timsort, or merge sort. This basic method performs badly in practice and is mostly used as an educational tool. It is called as bubble sort because, with each complete iteration, the largest element in the given array bubbles up to the last position or the highest index, similar to how a water bubble rises to the water’s surface. The largest element is moved to the last place in the first iteration, the second largest element is moved to the second last position in the second iteration, and so on. For array of size n, this process is repeated n – 1 times.

The biggest element bubbles up at the final place in the unsorted array at the end of each iteration. If the components being compared are out of sequence, they are switched. In general, sorting is accomplished by comparing neighboring elements A and A.

It compares the first and second elements of the array if the first element is greater than the second element, it will swap both elements, and then compare the second and third elements, and so on. It analyzes each element individually and sorts them based on their values. It is perhaps most simple sorting algorithm.īubble Sort is a simple method for sorting a given set of n elements provided in the form of an array with n elements. Bubble sort is comparison based sorting method, and also known as sinking sort.
