Solution
Bubble Sort repeatedly steps through the list, comparing adjacent elements and swapping them if they are in the wrong order, 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 until no more swaps are needed.
Example: For , Bubble Sort first compares and and swaps them, then compares the next pair ( and ), and so on. After several passes the list becomes .
Time complexity: . This makes Bubble Sort inefficient for large datasets. It is easy to understand and implement, so it is useful for teaching and for small lists.
Comparison with Merge Sort
Merge Sort uses divide and conquer: the list is split into smaller parts, each part is sorted, and the sorted parts are combined. Its time complexity is , a polynomial (tractable) bound. Sorting with Merge Sort or Quick Sort is treated as efficiently solvable in this chapter.
| Bubble Sort | Merge Sort | |
|---|---|---|
| Method | Adjacent swaps, repeated passes | Divide, sort parts, combine |
| Time complexity | ||
| Large datasets | Poor | Much better |
| Use | Teaching, small lists | Practical sorting of larger data |
For larger datasets, more efficient algorithms such as Merge Sort or Quick Sort save time and resources.