{"id":19149,"date":"2020-12-03T04:15:16","date_gmt":"2020-12-03T12:15:16","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19149"},"modified":"2023-12-01T04:05:34","modified_gmt":"2023-12-01T12:05:34","slug":"python-bubble-sort-a-how-to-guide","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/","title":{"rendered":"Python Bubble Sort: A How-To Guide"},"content":{"rendered":"\n<p><em>A Python bubble sort goes through a list and compares elements next to each other. If an element on the right is greater than one on the left, the elements are swapped. This happens until the list is sorted.<\/em><\/p>\n\n\n\n<p>Do you need to sort a list? The bubble sort has got your back. The bubble sort is a type of standard algorithm that sorts lists. It is perhaps the simplest sort out there, so it\u2019s perfect for beginners who are new to sorting algorithms!\n\n<\/p>\n\n\n\n<p>In this guide, we\u2019re going to discuss how bubble sorts work and how you can implement a Python bubble sort algorithm. We\u2019ll go through an example so that you understand how each part of a bubble sort works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Bubble Sorts<\/h2>\n\n\n\n<p>A bubble sort compares pairs of adjacent elements and swaps those elements if they are not in order. It is commonly implemented in Python to sort lists of unsorted numbers. Bubble sorts are a standard computer science algorithm.<\/p>\n\n\n\n<p>By using a bubble sort, you can sort data in either ascending or descending order. Starting from the first element in a list, a bubble sort will compare the first and second elements. If the first element is greater than the second, a swap occurs.\n\n<\/p>\n\n\n\n<p>This process is repeated until every item in a list is checked. Then, a bubble sort will loop through the list again. This occurs until no more swaps need to be performed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When Should You Use a Bubble Sort in Python?<\/h2>\n\n\n\n<p>Bubble sorts are a good sorting method to use when you\u2019re just starting to learn about sorting algorithms. A bubble sort is a simple way to sort a list of items that do not appear in order.\n\n<\/p>\n\n\n\n<p>Bubble sorts work best when you have a list with only a few objects. This is because when a bubble sort only has to make a few comparisons, it is very quick. When you need to sort a larger list, there are more efficient algorithms that you can use. Most developers would opt to use a method like an insertion sort to sort a longer list of items.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bubble Sort Algorithm Python: Walkthrough<\/h2>\n\n\n\n<p>Let\u2019s get into the weeds and start to walk through how a bubble sort works. We\u2019ll start with the following list, whose elements appear in the wrong order:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>7<\/td><td>19<\/td><td>4<\/td><td>12<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Our bubble sort starts by comparing the first and second elements in our list. If the first element is greater than the second, then we swap these two elements.\n\n<\/p>\n\n\n\n<p>In this example, we are going to compare 7 and 19. 7 is not greater than 19, so it stays in the same place. Our list now looks the same as it did before:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>7<\/td><td>19<\/td><td>4<\/td><td>12<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>We\u2019ll now go on to compare the second and third elements in our list. 19 is greater than 4, which means that we need to swap them. Our list now looks like this:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>7<\/td><td>4<\/td><td>19<\/td><td>12<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>We can now compare the third and fourth items in our list. 19 is greater than 12, so we swap the two numbers:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>7<\/td><td>4<\/td><td>12<\/td><td>19<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3 class=\"wp-block-heading\">Reaching the End of a List<\/h3>\n\n\n\n<p>Our list is already starting to look sorted. But we\u2019ve reached the end of our list and it is not sorted. What\u2019s going on? Bubble sorts make multiple passes through a list, which means that they keep running until every item in a list is sorted.\n\n<\/p>\n\n\n\n<p>Our bubble sort will start from the beginning again until the list is sorted. We call each time the list starts sorting values from the beginning <strong>a pass<\/strong>. In this example, our bubble sort will compare 7 and 4. 7 is greater than 4, so we swap the elements:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>4<\/td><td>7<\/td><td>12<\/td><td>19<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Our algorithm compares 7 and 12. No swap is necessary, so we\u2019ll move on. We compare 12 and 19. Again no swap is necessary. Now that we\u2019ve reached the end of our list, it\u2019s clear that no more swaps need to be made.\n\n<\/p>\n\n\n\n<p>Did you notice that our algorithm kept going even after our list sorted? That\u2019s because a bubble sort will continue to swap elements until it compares every item in a list for each item in the list. Our algorithm will not stop until every swap has taken place.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bubble Sort Python Program<\/h2>\n\n\n\n<p>Up until now we\u2019ve been swapping numbers in a table. It\u2019s true that we managed to sort our list, but we don\u2019t have to do this manually. Bubble sorts are a computing algorithm after all; let\u2019s get a computer to run the algorithm for us.<\/p>\n\n\n\n<p>Let\u2019s begin by writing a <a href=\"https:\/\/careerkarma.com\/blog\/python-functions\/\">Python function<\/a> that sorts a list of numbers in ascending order:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def sortList(array):\n\tfor item in range(len(array)):\n\t\tfor j in range(0, (len(array) - item - 1)):\n\t\t\tif array[j] &gt; array[j + 1]:\n\t\t\t\t(array[j], array[j + 1]) = (array[j + 1], array[j])<\/pre><\/div>\n\n\n\n<p>Our algorithm starts with a for loop. This loop iterates through every item in our array. Then, we use another for loop to compare all the items in our array with each other.\n\n<\/p>\n\n\n\n<p>In our code, we\u2019ve defined a <a href=\"https:\/\/careerkarma.com\/blog\/python-if-else\/\">Python \u201cif\u201d statement<\/a> which checks whether a given item is larger than the next item in the list. This \u201cif\u201d statement will perform comparisons such as: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Is the first item in the list greater than the second?<\/li><li>Is the second item in the list greater than the third?<\/li><\/ul>\n\n\n\n<p>Our code isn\u2019t finished yet. If you try to run the above Python program, nothing will happen. We\u2019ve got to call our function and give it some data:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>numbers = [7, 19, 4, 12]\nsortList(numbers)\nprint(&quot;List in order:&quot;, numbers)\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>List in order: [4, 7, 12, 19]<\/pre><\/div>\n\n\n\n<p>We did it! Our <a href=\"https:\/\/careerkarma.com\/blog\/python-array\/\">Python array<\/a> is sorted in ascending order! You can use a bubble sort to sort a list in descending order. To do so, swap the greater than sign with a lesser than sign in the Python \u201cif\u201d statement:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>if array[j] &lt; array[j + 1]:\n<\/pre><\/div>\n\n\n\n<p>When we run our program with this revised line of code, the following is returned:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>List in order: [19, 12, 7, 4]<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Optimizing the Bubble Sort<\/h2>\n\n\n\n<p>Earlier on we talked about how every possible comparison is made even if our list is sorted. This makes our bubble sort quite inefficient: it keeps going even after the list is sorted.\n\n<\/p>\n\n\n\n<p>While it doesn\u2019t make a big difference in this example, at scale this could impact the execution time of a program. That\u2019s where the optimized bubble sort comes in.\n\n<\/p>\n\n\n\n<p>We can optimize our bubble sort by writing a new variable. Let\u2019s call it <em>swap<\/em>. This variable will track whether any swaps have taken place in a<a href=\"https:\/\/careerkarma.com\/blog\/python-for-loop\/\"><\/a> <a href=\"https:\/\/careerkarma.com\/blog\/python-for-loop\/\">Python for loop<\/a>. If this variable is set to false, then it means that our list is sorted. No more iterations need to happen.\n\n<\/p>\n\n\n\n<p>Let\u2019s revise our sortList function from earlier:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def sortList(array):\n\tfor item in range(len(array)):\n\t\tswap = True\n\n\t\tfor j in range(0, (len(array) - item - 1)):\n\t\t\tif array[j] &gt; array[j + 1]:\n\t\t\t\t(array[j], array[j + 1]) = (array[j + 1], array[j])\n\n\t\t\t\tswap = False\n\n\t\t\t\tif swap == True:\n\t\t\t\t\tbreak<\/pre><\/div>\n\n\n\n<p>We\u2019ve defined a variable called <em>swap<\/em> which has the default value: True. This is contained within our first <em>for<\/em> loop because it keeps track of whether a swap has occurred on each pass through the list. If our array makes a comparison, the value of <em>swap<\/em> is set to False. <\/p>\n\n\n\n<p>If there is no swap made in the last swap, then the array is already sorted. Our list will then check whether <em>swap<\/em> is equal to True. If it is, our program will stop executing.\n\n<\/p>\n\n\n\n<p>Let\u2019s run our code again:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>List in order: [4, 7, 12, 19]<\/pre><\/div>\n\n\n\n<p>Our data has been sorted the same way but our algorithm is now faster and more efficient. Our algorithm now stops as soon as all the items in the list have been sorted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complexity Analysis<\/h2>\n\n\n\n<p>The average time complexity of the bubble sort is O(n^2). This happens when the elements in an array are unsorted.<\/p>\n\n\n\n<p>In the worst case, a bubble sort performs at O(n^2). This happens when an array is already in ascending or descending order and needs to be sorted the opposite way. In the best case, this algorithm will perform at O(n). This happens if an array is already sorted.<\/p>\n\n\n\n<p>To learn more about algorithm complexity, check out our Career Karma <a href=\"https:\/\/careerkarma.com\/blog\/big-o-notation-time\/\">Big O Notation guide<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Bubble sorts provide a simple way in which you can sort a list of data. They can be used to sort data in either ascending or descending order.\n\n<\/p>\n\n\n\n<p>This algorithm is most commonly used when you need to sort a small list. Bubble sorts are a good introduction to sorting algorithms. You can use them to help familiarize yourself with algorithms before you learn about more advanced methods of sorting, such as an insertion sort.<\/p>\n\n\n\n<p>For expert guidance on Python resources and courses, check out our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"A Python bubble sort goes through a list and compares elements next to each other. If an element on the right is greater than one on the left, the elements are swapped. This happens until the list is sorted. Do you need to sort a list? The bubble sort has got your back. The bubble&hellip;","protected":false},"author":240,"featured_media":3371,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-19149","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Bubble Sort: A How-To Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Bubble sorts compare pairs of adjacent elements and swap those elements if they are not in order. On Career Karma, learn how to implement a bubble sort in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Bubble Sort: A How-To Guide\" \/>\n<meta property=\"og:description\" content=\"Bubble sorts compare pairs of adjacent elements and swap those elements if they are not in order. On Career Karma, learn how to implement a bubble sort in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-03T12:15:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:05:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/hitesh-choudhary-666995-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Bubble Sort: A How-To Guide\",\"datePublished\":\"2020-12-03T12:15:16+00:00\",\"dateModified\":\"2023-12-01T12:05:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/\"},\"wordCount\":1382,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/hitesh-choudhary-666995-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/\",\"name\":\"Python Bubble Sort: A How-To Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/hitesh-choudhary-666995-unsplash.jpg\",\"datePublished\":\"2020-12-03T12:15:16+00:00\",\"dateModified\":\"2023-12-01T12:05:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Bubble sorts compare pairs of adjacent elements and swap those elements if they are not in order. On Career Karma, learn how to implement a bubble sort in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/hitesh-choudhary-666995-unsplash.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/hitesh-choudhary-666995-unsplash.jpg\",\"width\":1200,\"height\":675},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-bubble-sort-a-how-to-guide\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Bubble Sort: A How-To Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/jamesgallagher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Bubble Sort: A How-To Guide | Career Karma","description":"Bubble sorts compare pairs of adjacent elements and swap those elements if they are not in order. On Career Karma, learn how to implement a bubble sort in Python.","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:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/","og_locale":"en_US","og_type":"article","og_title":"Python Bubble Sort: A How-To Guide","og_description":"Bubble sorts compare pairs of adjacent elements and swap those elements if they are not in order. On Career Karma, learn how to implement a bubble sort in Python.","og_url":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-12-03T12:15:16+00:00","article_modified_time":"2023-12-01T12:05:34+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/hitesh-choudhary-666995-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Bubble Sort: A How-To Guide","datePublished":"2020-12-03T12:15:16+00:00","dateModified":"2023-12-01T12:05:34+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/"},"wordCount":1382,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/hitesh-choudhary-666995-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/","url":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/","name":"Python Bubble Sort: A How-To Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/hitesh-choudhary-666995-unsplash.jpg","datePublished":"2020-12-03T12:15:16+00:00","dateModified":"2023-12-01T12:05:34+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Bubble sorts compare pairs of adjacent elements and swap those elements if they are not in order. On Career Karma, learn how to implement a bubble sort in Python.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/hitesh-choudhary-666995-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/hitesh-choudhary-666995-unsplash.jpg","width":1200,"height":675},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-bubble-sort-a-how-to-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Bubble Sort: A How-To Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19149"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19149\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/3371"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}