

{"id":123666,"date":"2023-11-04T12:24:43","date_gmt":"2023-11-04T06:54:43","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123666"},"modified":"2024-02-28T12:09:40","modified_gmt":"2024-02-28T06:39:40","slug":"python-program-for-bubble-sort","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/","title":{"rendered":"Python Program for Bubble Sort"},"content":{"rendered":"<p>In this article, we will explore a Python program that implements the Bubble Sort algorithm, a simple sorting algorithm used to arrange elements in ascending order. Sorting is a fundamental operation in computer science and is essential for various applications. The program demonstrates the step-by-step process of sorting an array using the Bubble Sort technique.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Fundamental Python Knowledge (Variables, Data Types, Loops, Syntax)<\/li>\n<li>Familiarity with Basic Array Concepts (Declaration, Manipulation, Iteration)<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<h4>Bubble Sort Algorithm<\/h4>\n<p>The program begins by taking user input for the limit of the array and then prompts the user to enter elements. It uses the Bubble Sort algorithm to iteratively compare adjacent elements and swap them if they are in the wrong order. Until the entire array is sorted, these steps are repeated. The article explains the logic behind the Bubble Sort algorithm and its step-by-step execution.<\/p>\n<p>Furthermore, the article delves into the underlying logic behind the Bubble Sort algorithm and meticulously outlines its step-by-step execution. Readers gain a comprehensive understanding of how this sorting method operates, making it a valuable resource for learning about sorting algorithms and enhancing one&#8217;s problem-solving skills in Python.<\/p>\n<h4><strong>Program Code:<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Program for Bubble sort in Python\r\n\r\n# Import the array module to use arrays\r\nimport array as ar\r\n\r\n# Create an empty integer array\r\nmyar = ar.array('i', [])\r\n\r\n# Get the limit of the array from the user\r\nn = int(input(\"Enter limit of array\"))\r\n\r\n# Ask the user to add components to the array.\r\nprint(\"Enter elements in array\")\r\nfor i in range(n):\r\n    x = int(input())\r\n    myar.append(x)\r\n\r\n# Perform Bubble Sort on the array\r\nfor i in range(n):\r\n    for j in range(n - i - 1):\r\n        # Compare adjacent elements and swap if they are in the wrong order\r\n        if myar[j] &gt; myar[j + 1]:\r\n            temp = myar[j]\r\n            myar[j] = myar[j + 1]\r\n            myar[j + 1] = temp\r\n\r\n# Print the sorted array\r\nprint(\"Sorted Array\")\r\nprint(myar)<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>Enter limit of array5<br \/>\nEnter elements in array<br \/>\n3<br \/>\n1<br \/>\n9<br \/>\n5<br \/>\n2<br \/>\nSorted Array<br \/>\narray(&#8216;i&#8217;, [1, 2, 3, 5, 9])<\/p>\n<\/div>\n<h4>Code Explanation:<\/h4>\n<p>1. Import array module to support dynamic arrays<br \/>\n2. Create an empty integer array<br \/>\n3. Take size of array input from user<br \/>\n4. Take array elements as input from user in a loop<br \/>\n5. Append each element to the array<br \/>\n6. Outer loop goes from 0 to n-1<\/p>\n<ul>\n<li>Inner loop goes from 0 to n-1-i<\/li>\n<li>Compare adjacent elements at index j and j+1<\/li>\n<li>If element at j is greater than element at j+1<\/li>\n<li>Swap the elements using a temporary variable<\/li>\n<\/ul>\n<p>7. After inner loop completes one iteration, largest element reached at end<br \/>\n8. Print the sorted array<\/p>\n<h3>Summary<\/h3>\n<p>In summary, this Python program serves as a practical example of implementing the Bubble Sort algorithm for sorting an array. Understanding sorting algorithms is fundamental in computer science and provides a solid foundation for more advanced algorithms and data structures. Proficiency in sorting techniques equips individuals to efficiently tackle computational challenges and forms the basis for exploring more advanced problem-solving strategies within the field of programming and computer science.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that implements the Bubble Sort algorithm, a simple sorting algorithm used to arrange elements in ascending order. Sorting is a fundamental operation in computer science&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[24493,28705,10333,28706,28626,28704],"class_list":["post-123666","post","type-post","status-publish","format-standard","hentry","category-python","tag-bubble-sort","tag-bubble-sort-in-python","tag-python","tag-python-bubble-sort","tag-python-practical","tag-python-program-for-bubble-sort"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program for Bubble Sort - DataFlair<\/title>\n<meta name=\"description\" content=\"This Python program serves as a practical example of implementing the Bubble Sort algorithm for sorting an array.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program for Bubble Sort - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This Python program serves as a practical example of implementing the Bubble Sort algorithm for sorting an array.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T06:54:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:39:40+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program for Bubble Sort - DataFlair","description":"This Python program serves as a practical example of implementing the Bubble Sort algorithm for sorting an array.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/","og_locale":"en_US","og_type":"article","og_title":"Python Program for Bubble Sort - DataFlair","og_description":"This Python program serves as a practical example of implementing the Bubble Sort algorithm for sorting an array.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-04T06:54:43+00:00","article_modified_time":"2024-02-28T06:39:40+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program for Bubble Sort","datePublished":"2023-11-04T06:54:43+00:00","dateModified":"2024-02-28T06:39:40+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/"},"wordCount":371,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["Bubble Sort","bubble sort in python","Python","python bubble sort","python practical","python program for bubble sort"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/","url":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/","name":"Python Program for Bubble Sort - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-04T06:54:43+00:00","dateModified":"2024-02-28T06:39:40+00:00","description":"This Python program serves as a practical example of implementing the Bubble Sort algorithm for sorting an array.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-for-bubble-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program for Bubble Sort"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123666"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123666\/revisions"}],"predecessor-version":[{"id":134153,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123666\/revisions\/134153"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}