3.7.1.1 Bubble Sort
Algorithms and Problem Solving
Bubble Sort is one of the simplest sorting algorithms. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. This process is repeated until the list is sorted.
Process:
- Start from the beginning of the list.
- Compare each pair of adjacent elements.
- Swap them if they are in the wrong order.
- Continue the process until no more swaps are needed.
Example: Consider the list [5,3,8,4,2]. Bubble Sort will compare 5 and 3, swap them, then move to the next pair, and so on. After several passes through the list, the algorithm will sort the list as [2,3,4,5,8].
Complexity: The time complexity of Bubble Sort is O(n²), making it inefficient for large datasets. However, it is easy to understand and implement, making it useful for educational purposes and small datasets.