Bubble Sort
Bubble Sort is one of the simplest sorting algorithms. Despite its simplicity, it is not the most efficient for large datasets. The algorithm repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The process continues until no more swaps are needed, indicating that the list is sorted.
What is Bubble Sort?
Bubble Sort is a comparison-based sorting algorithm that repeatedly compares adjacent items in a list and swaps them if they are in the wrong order. The process is repeated for each element in the list until the list is sorted. Each pass through the list moves the next largest element to its correct position, like a bubble rising to the surface of water, which is where the algorithm gets its name.
Key Properties of Bubble Sort:
- Stable Sort: Bubble Sort does not change the relative order of elements with equal values.
- In-Place: It does not require any additional storage or memory, as the sorting is done within the list.
- Simple: It is easy to understand and implement, making it useful for educational purposes.
How Does Bubble Sort Work?
- Compare Adjacent Elements: Start at the beginning of the list, comparing each pair of adjacent elements.
- Swap if Necessary: If the current element is greater than the next element, swap them.
- Repeat for All Elements: Continue this process for the entire list, which bubbles the largest element to the end.
- Repeat the Process: For the next pass, the comparison is made up to the second-to-last element, as the last element is already sorted. Continue until no more swaps are made, which means the list is sorted.
Bubble Sort Algorithm: Step-by-Step
- Initialization: Start with the first element of the list.
- Comparison: Compare the current element with the next one.
- Swap: If the current element is larger than the next one, swap them.
- Repeat: Move to the next pair of elements and repeat the comparison and swapping process.
- End Condition: If a complete pass through the list occurs without any swaps, the list is sorted.
Bubble Sort: Python Code Implementation
Here’s a simple Python implementation of the Bubble Sort algorithm:
def bubble_sort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
swapped = False # Flag to optimize and check if swapping happened
# Last i elements are already sorted
for j in range(0, n-i-1):
# Swap if the element found is greater than the next element
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
# If no two elements were swapped in the inner loop, then the list is sorted
if not swapped:
break
return arr
# Example usage:
arr = [64, 34, 25, 12, 22, 11, 90]
print("Unsorted Array:", arr)
sorted_arr = bubble_sort(arr)
print("Sorted Array:", sorted_arr)
Output:
Unsorted Array: [64, 34, 25, 12, 22, 11, 90]
Sorted Array: [11, 12, 22, 25, 34, 64, 90]
Explanation:
- Outer loop: It goes through each element of the array.
- Inner loop: It compares each pair of adjacent elements and swaps them if they are in the wrong order.
- Optimization: The
swapped
flag checks whether a swap was made during the inner loop. If no swaps are made in a full pass, the list is already sorted, and the algorithm terminates early.
Time and Space Complexity of Bubble Sort
Time Complexity:
- Worst Case: O(n2) – This occurs when the list is in reverse order.
- Best Case: O(n) – This occurs when the list is already sorted, and the algorithm performs only one pass with no swaps.
- Average Case: O(n2) – On average, the algorithm will need to perform about half of the comparisons and swaps.
Space Complexity:
- Space Complexity: O(1) – Since Bubble Sort is an in-place sorting algorithm, it does not require additional storage space apart from the input list.
Advantages of Bubble Sort
- Simple to Implement: The algorithm is straightforward and easy to understand, which makes it great for educational purposes.
- In-place Sorting: Bubble Sort requires no additional memory apart from the input array, making it space efficient.
- Stable: It maintains the relative order of equal elements, which is important in certain applications.
Disadvantages of Bubble Sort
- Inefficient for Large Lists: The time complexity of O(n2) makes it impractical for large datasets compared to more efficient sorting algorithms like Merge Sort or Quick Sort.
- Slow Performance: Even though it has an early termination condition, it still takes a lot of time for large arrays.
- Redundant Comparisons: In the worst case, the algorithm compares and swaps elements even if the list is already sorted, which can be avoided with better algorithms.
When to Use Bubble Sort?
- Educational Purposes: Bubble Sort is often used to introduce students to sorting algorithms due to its simplicity.
- Small Lists: It can be used for small datasets where the inefficiency of the algorithm is not as noticeable.
- When Stability is Required: Since Bubble Sort is a stable sorting algorithm, it might be used when maintaining the order of equal elements is important.