Skip to main content

Posts

Showing posts with the label Bubble Sort

Bubble Sort

Bubble sort is a simple sorting algorithm in which adjacent elements are compared to each other and swapped if the elements are not in intended order. It is a comparison based algorithm. This algorithm is not suitable for large data because of its not so good worst and average case time complexity O(n 2 ) , n is the number of elements. Illustration: Algorithm: for all elements in list     for adjacent elements of unsorted portion of list         if leftElement > rightElement             swap the elements Time Complexity: Worst Case:  O(n 2 ) Average Case:  O(n 2 ) Best Case:  O(n) Space Complexity: O(1) Program for Bubble Sort: Output: