Write a Python program to implement the Bubble Sort

Write a Python program to implement the Bubble Sort

def bubble_sort(arr):
    """
    Sorts a list using the Bubble Sort algorithm.
    """
    n = len(arr)
    # Traverse through all elements in the list
    for i in range(n):
        # Flag to optimize: check if any swaps happened in this pass
        swapped = False
        # Last i elements are already in place, so we can reduce the inner loop
        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 by inner loop, then the list is sorted
        if not swapped:
            break
    return arr

# --- Example Usage ---
# List of ages of Indian cricketers
ages = [45, 38, 29, 34, 41, 25, 22]
print("Original list:", ages)

# Sorting the list
sorted_ages = bubble_sort(ages.copy())  # Using copy to keep original unchanged for display
print("Sorted list (Ascending):", sorted_ages)

# Example with a list of names (strings)
names = ["Virat", "Rohit", "Jasprit", "Ravindra", "Mohammed", "Axar"]
print("Original names:", names)
sorted_names = bubble_sort(names.copy())
print("Sorted names (Alphabetical):", sorted_names)