![]() The best case complexity (When the list is in order) of the Bubble sort is of order n ( O(n)), and worst case complexity is O(n 2). This algorithm is the slowest sorting algorithm. And after each iteration, the number of comparisons decreases and at last iteration only one comparison takes place. The largest element would be at nth position after the first iteration. The number of comparisons in the first iteration are n-1 where n is the number elements in an array. Similarly, Second and third element are compared and swapped, and this comparing and swapping go on to the end of the list. In simple words, it compares the first and second element of the list and swaps it unless they are out of specific order. Improved efficiency as compared to bubble sortīubble sort is the simplest iterative algorithm operates by comparing each item or element with the item next to it and swapping them if needed. Largest element is selected and swapped with the last element (in case of ascending order). Generally, most stable and fast algorithms use additional memory. An algorithm is considered to be steady the elements with the same key occurring in the same order as they were occurring before sorting in the list or array. ![]() Another considerable difference between the two is that bubble sort is stable algorithm while selection sort is an unstable algorithm.
0 Comments
Leave a Reply. |