
Bubble Sort in Python
Bubble Sort in Python
A list is regularly iterated over using the straightforward yet efficient bubble sort sorting algorithm, which compares neighboring elements and swaps them if they are out of order. The reason it’s called “Bubble Sort” is that, like bubbles rising to the top of the list, smaller components gradually ascend there.Although Bubble Sort is not the most efficient sorting technique, it is easy to grasp and implement, making it an excellent starting point for understanding sorting algorithms.
Complete Python Course with Advance topics:-Click here
Understanding Bubble Sort
Bubble Sort’s basic concept is simple: it repeatedly iterates through the list, switching neighboring members if they are out of order. The process continues until the list is completely sorted.
Steps of the Bubble Sort Algorithm:
- Go over the list again: Go through the list starting at the beginning.
- Examine neighboring elements and switch them if the current element is larger than the subsequent one.
- Do it again: Until the end of the list is reached, keep comparing and switching.
- Iterate further by repeating the previous stages until the list is sorted and no more swaps are required.
Despite its simplicity, Bubble Sort has a significant limitation—it performs poorly on large datasets. It is significantly slower than more sophisticated sorting algorithms like Quick Sort or Merge Sort, with a worst-case time complexity of O(n²).
Python Implementation of Bubble Sort
Let’s now put Bubble Sort into practice in Python and see how it functions with an example list:
# Function to implement Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
# If no elements were swapped, the list is already sorted
if not swapped:
break
# Example usage
if __name__ == "__main__":
sample_list = [64, 34, 25, 12, 22, 11, 90]
print("Original List:", sample_list)
bubble_sort(sample_list)
print("Sorted List:", sample_list)
Output:
Original List: [64, 34, 25, 12, 22, 11, 90]
Sorted List: [11, 12, 22, 25, 34, 64, 90]
Explanation:
- The algorithm compares and switches neighboring elements as it iterates across the list.
- In every pass, the largest unsorted element is moved to its proper location.
- The procedure keeps on until the list is completely sorted and no swaps are required.
- The
swapped
flag ensures that the algorithm stops early if the list gets sorted before completing all passes.
Time Complexity Analysis
Understanding time complexity is essential to evaluate an algorithm’s efficiency.Bubble Sort is ineffective for large datasets due to its O(n²) worst-case and average-case time complexity.
Breakdown of Time Complexity:
- Best case (already sorted list): O(n) (with optimization using the swapped flag)
- Average case: O(n²)
- Worst case (reverse sorted list): O(n²)
Since Bubble Sort compares and swaps elements in a nested loop, it performs poorly on large lists. More efficient sorting algorithms, such as Merge Sort (O(n log n)) or Quick Sort (O(n log n)), are preferred for real-world applications.
Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE
Conclusion
This article examined Bubble Sort, a basic sorting method that alternates neighboring entries periodically until the list is sorted. We explored the time complexity of Bubble Sort, examined its output, and implemented it in Python. While Bubble Sort is easy to understand, its inefficiency on large datasets makes it less suitable for practical applications compared to more advanced sorting algorithms.
For more insightful programming tutorials and project ideas, stay tuned to UpdateGadh!
bubble sort in python with example
bubble sort in python using for loop
selection sort in python
Bubble Sort in Python
insertion sort in python
bubble sort in python using list
Bubble Sort in Python with full example
bubble sort in python w3schools
Bubble Sort in Python algorithm
Post Comment