Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm is named for the way smaller or larger elements "bubble" to the top of the list.
Here is an example of Bubble Sort in action:
1. Start with an unsorted list of integers: [5, 2, 8, 4, 9, 1]
2. Compare the first two elements (5 and 2). Since 5 is greater than 2, swap them: [2, 5, 8, 4, 9, 1]
3. Compare the next two elements (5 and 8). Since they are in the correct order, do not swap them: [2, 5, 8, 4, 9, 1]
4. Continue comparing and swapping adjacent elements until reaching the end of the list. The list now looks like this: [2, 5, 4, 8, 1, 9]
5. Repeat the process until the list is completely sorted. After the second pass, the list looks like this: [2, 4, 5, 1, 8, 9]. After the third pass, the list is sorted: [1, 2, 4, 5, 8, 9].
Although Bubble Sort is easy to understand and implement, it is not very efficient for large data sets. Its average and worst-case time complexity is O(n^2). There are other sorting algorithms that are more efficient, such as Quick Sort and Merge Sort.