{"id":1011,"date":"2020-05-25T04:08:24","date_gmt":"2020-05-25T04:08:24","guid":{"rendered":"https:\/\/www.projectimmerse.com\/blog\/?p=1011"},"modified":"2020-06-15T06:58:39","modified_gmt":"2020-06-15T06:58:39","slug":"bubble-sort-algorithm-using-python","status":"publish","type":"post","link":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/","title":{"rendered":"Bubble Sort Algorithm using Python"},"content":{"rendered":"<p>Hey!!! What&#8217;s up everyone?! I hope your week has been great! I have been working on tirelessly on my projects, so I am going to take a short break, and write up this article!<\/p>\n<p>For today, I want to touch base on a classic algorithm that students have seen over the years, and it is called Bubble Sort Algorithm! Now, before I get started, there are obviously much better sorting algorithms are there, but for the sake of improving our critical thinking skills, Bubble Sort Algorithm is a good one!<\/p>\n<p><!--more--><\/p>\n<p>Now, what is Bubble Sort Algorithm? It is the simplest sorting algorithm that works by swapping adjacent elements, until every element is sorted in the right order. I&#8217;m going to reference to the demonstration from GeeksforGeeks to help explain what the algorithm is about!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1012\" src=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-24-at-8.44.59-PM-300x191.png\" alt=\"\" width=\"300\" height=\"191\" srcset=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-24-at-8.44.59-PM-300x191.png 300w, https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-24-at-8.44.59-PM.png 726w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Now that you see how the algorithm works, let&#8217;s get to the code!<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# bubble sort algorithm function\r\ndef bubbleSort(my_list):\r\n\r\n    # first loop\r\n    for i in range(len(my_list)):\r\n\r\n        # second loop\r\n        for j in range(0, len(my_list)-1): \r\n\r\n            # swapping logic\r\n            if my_list&#x5B;j] &gt; my_list&#x5B;j+1]:\r\n                t = my_list&#x5B;j]\r\n                my_list&#x5B;j] = my_list&#x5B;j+1]\r\n                my_list&#x5B;j+1] = t\r\n\r\n    # return the list\r\n    return my_list\r\n\r\nmy_list = &#x5B;4, 30, 22, 27, 41, 1, 5, 80]\r\nprint(&quot;Before sorted: &quot;, end='')\r\nprint(my_list)\r\nprint(&quot;After sorted: &quot;, end='')\r\nprint(bubbleSort(my_list))\r\n<\/pre>\n<p>The important thing to note about this algorithm is you need to think of how to go over different passes of the sequence, hence, you have to put the logic inside the nested loops. Another good way to solve this particular problem is you could also use recursive function, so if you want a challenge, solve it using recursion!<\/p>\n<p>Anyways, I&#8217;m going to stop here for the day. If you have any question, don&#8217;t hesitate to ask!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey!!! What&#8217;s up everyone?! I hope your week has been great! I have been working on tirelessly on my projects, so I am going to take a short break, and write up this article! For today, I want to touch base on a classic algorithm that students have seen over the years, and it is &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bubble Sort Algorithm using Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1086,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[40,39],"class_list":["post-1011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-algorithms","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bubble Sort Algorithm using Python - Project Immerse<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bubble Sort Algorithm using Python - Project Immerse\" \/>\n<meta property=\"og:description\" content=\"Hey!!! What&#8217;s up everyone?! I hope your week has been great! I have been working on tirelessly on my projects, so I am going to take a short break, and write up this article! For today, I want to touch base on a classic algorithm that students have seen over the years, and it is &hellip; Continue reading &quot;Bubble Sort Algorithm using Python&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Project Immerse\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-25T04:08:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-15T06:58:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/bubble-sort-algorithm-using-python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"projectimmerse\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"projectimmerse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/\"},\"author\":{\"name\":\"projectimmerse\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"headline\":\"Bubble Sort Algorithm using Python\",\"datePublished\":\"2020-05-25T04:08:24+00:00\",\"dateModified\":\"2020-06-15T06:58:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/\"},\"wordCount\":316,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/bubble-sort-algorithm-using-python.png\",\"keywords\":[\"algorithms\",\"python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/\",\"name\":\"Bubble Sort Algorithm using Python - Project Immerse\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/bubble-sort-algorithm-using-python.png\",\"datePublished\":\"2020-05-25T04:08:24+00:00\",\"dateModified\":\"2020-06-15T06:58:39+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/bubble-sort-algorithm-using-python.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/bubble-sort-algorithm-using-python.png\",\"width\":750,\"height\":750,\"caption\":\"Bubble Sort Algorithm Using Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/bubble-sort-algorithm-using-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bubble Sort Algorithm using Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\",\"name\":\"Project Immerse\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\",\"name\":\"projectimmerse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"caption\":\"projectimmerse\"},\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/author\\\/projectimmerse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bubble Sort Algorithm using Python - Project Immerse","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:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/","og_locale":"en_US","og_type":"article","og_title":"Bubble Sort Algorithm using Python - Project Immerse","og_description":"Hey!!! What&#8217;s up everyone?! I hope your week has been great! I have been working on tirelessly on my projects, so I am going to take a short break, and write up this article! For today, I want to touch base on a classic algorithm that students have seen over the years, and it is &hellip; Continue reading \"Bubble Sort Algorithm using Python\"","og_url":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/","og_site_name":"Project Immerse","article_published_time":"2020-05-25T04:08:24+00:00","article_modified_time":"2020-06-15T06:58:39+00:00","og_image":[{"width":750,"height":750,"url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/bubble-sort-algorithm-using-python.png","type":"image\/png"}],"author":"projectimmerse","twitter_card":"summary_large_image","twitter_misc":{"Written by":"projectimmerse","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#article","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/"},"author":{"name":"projectimmerse","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"headline":"Bubble Sort Algorithm using Python","datePublished":"2020-05-25T04:08:24+00:00","dateModified":"2020-06-15T06:58:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/"},"wordCount":316,"commentCount":0,"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/bubble-sort-algorithm-using-python.png","keywords":["algorithms","python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/","url":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/","name":"Bubble Sort Algorithm using Python - Project Immerse","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#primaryimage"},"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/bubble-sort-algorithm-using-python.png","datePublished":"2020-05-25T04:08:24+00:00","dateModified":"2020-06-15T06:58:39+00:00","author":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"breadcrumb":{"@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#primaryimage","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/bubble-sort-algorithm-using-python.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2020\/05\/bubble-sort-algorithm-using-python.png","width":750,"height":750,"caption":"Bubble Sort Algorithm Using Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.projectimmerse.com\/blog\/bubble-sort-algorithm-using-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.projectimmerse.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Bubble Sort Algorithm using Python"}]},{"@type":"WebSite","@id":"https:\/\/www.projectimmerse.com\/blog\/#website","url":"https:\/\/www.projectimmerse.com\/blog\/","name":"Project Immerse","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.projectimmerse.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5","name":"projectimmerse","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","caption":"projectimmerse"},"url":"https:\/\/www.projectimmerse.com\/blog\/author\/projectimmerse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/1011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/comments?post=1011"}],"version-history":[{"count":8,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/1011\/revisions"}],"predecessor-version":[{"id":1080,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/1011\/revisions\/1080"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media\/1086"}],"wp:attachment":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media?parent=1011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/categories?post=1011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/tags?post=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}