{"id":169,"date":"2026-09-03T08:48:05","date_gmt":"2026-09-03T08:48:05","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=169"},"modified":"2026-09-03T08:48:05","modified_gmt":"2026-09-03T08:48:05","slug":"write-a-python-program-to-implement-the-bubble-sort","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2026\/09\/03\/write-a-python-program-to-implement-the-bubble-sort\/","title":{"rendered":"Write a Python program to implement the Bubble Sort"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def bubble_sort(arr):\n    \"\"\"\n    Sorts a list using the Bubble Sort algorithm.\n    \"\"\"\n    n = len(arr)\n    # Traverse through all elements in the list\n    for i in range(n):\n        # Flag to optimize: check if any swaps happened in this pass\n        swapped = False\n        # Last i elements are already in place, so we can reduce the inner loop\n        for j in range(0, n - i - 1):\n            # Swap if the element found is greater than the next element\n            if arr[j] &gt; arr[j + 1]:\n                arr[j], arr[j + 1] = arr[j + 1], arr[j]\n                swapped = True\n        # If no two elements were swapped by inner loop, then the list is sorted\n        if not swapped:\n            break\n    return arr\n\n# --- Example Usage ---\n# List of ages of Indian cricketers\nages = [45, 38, 29, 34, 41, 25, 22]\nprint(\"Original list:\", ages)\n\n# Sorting the list\nsorted_ages = bubble_sort(ages.copy())  # Using copy to keep original unchanged for display\nprint(\"Sorted list (Ascending):\", sorted_ages)\n\n# Example with a list of names (strings)\nnames = [\"Virat\", \"Rohit\", \"Jasprit\", \"Ravindra\", \"Mohammed\", \"Axar\"]\nprint(\"Original names:\", names)\nsorted_names = bubble_sort(names.copy())\nprint(\"Sorted names (Alphabetical):\", sorted_names)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>def bubble_sort(arr): &#8220;&#8221;&#8221; Sorts a list using the Bubble Sort algorithm. &#8220;&#8221;&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":96,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,3],"tags":[52,25,26,45],"class_list":["post-169","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aiopd","category-bca","tag-aiopd","tag-bca","tag-bca-paper-solution","tag-python"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions\/170"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/96"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}