{"id":1044,"date":"2023-11-04T08:32:20","date_gmt":"2023-11-04T08:32:20","guid":{"rendered":"https:\/\/learnlearn.uk\/igcsecs\/?page_id=1044"},"modified":"2023-11-04T08:34:17","modified_gmt":"2023-11-04T08:34:17","slug":"bubble-sort","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/","title":{"rendered":"Bubble Sort"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Bubble Sort<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Introduction to Bubble Sort<\/h3>\n<p class=\"\">Bubble sort works by iterating through the array repeatedly, comparing adjacent elements and swapping them if they are in the wrong order.<\/p>\n<p class=\"\">The algorithm keeps track of the sorted partition of the array and continues sorting the remaining unsorted partition until the entire array is sorted.<\/p>\n<p class=\"\">This process is repeated until the array is fully sorted. It starts with the first pair of elements and continues until the last pair.<\/p>\n<p class=\"\">It is named as &#8220;bubble sort&#8221; because smaller elements &#8220;bubble&#8221; to the top of the list.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/revise.learnlearn.uk\/django-summernote\/2023-08-27\/177aadce-dec7-40fd-aef2-3033cdecac67.gif\" \/><\/p>\n\n<\/div><h2 class=\"tabtitle\">Demo<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Bubble Sort Demonstration<\/h3>\n<p><iframe loading=\"lazy\" src=\"https:\/\/scratch.mit.edu\/projects\/444092744\/embed\" width=\"800\" height=\"600\" frameborder=\"0\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n\n<\/div><h2 class=\"tabtitle\">Example<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Example<br \/>\nLet&#8217;s say we have an array [5, 2, 8, 3, 1].<\/h3>\n<p>In the first iteration, the algorithm compares 5 and 2, swaps them to get [2, 5, 8, 3, 1].<\/p>\n<p>Then it compares 5 and 8, no swap needed. It goes on like this until it reaches the end of the array. After the first iteration, the largest element (8) is in its correct position at the end of the array.<\/p>\n<p>The remaining unsorted partition is [2, 3, 1]. The algorithm continues with the second iteration, again comparing and swapping elements until the array is sorted.<\/p>\n<p>The final sorted array will be [1, 2, 3, 5, 8].<\/p>\n<p>To see bubble sort in action take a look at this Scratch project.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Pros &amp; Cons<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Pros and Cons of Bubble Sort<\/h3>\n<h3 class=\"\">Pros<\/h3>\n<ul>\n<li>Simple and easy to understand<\/li>\n<li>Because it&#8217;s an\u00a0<b>in-place sorting algorithm<\/b>\u00a0it requires minimal additional memory space<\/li>\n<li>Works well for small lists or nearly sorted lists<\/li>\n<\/ul>\n<h3 class=\"\">Cons<\/h3>\n<ul>\n<li>Not efficient for large lists<\/li>\n<li>Time complexity is O(n^2), which means the execution time increases rapidly with the growing size of the array<\/li>\n<li>Not suitable for real-world applications with large datasets<\/li>\n<\/ul>\n\n<\/div><h2 class=\"tabtitle\">Python Code<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Python Code for Bubble Sort<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">bubble_sort<\/span>(arr):\r\n    n = <span style=\"color: #00aaaa;\">len<\/span>(arr)\r\n    \r\n    <span style=\"color: #0000aa;\">for<\/span> i <span style=\"color: #0000aa;\">in<\/span> <span style=\"color: #00aaaa;\">range<\/span>(n):\r\n        <span style=\"color: #aaaaaa; font-style: italic;\"># Flag to optimize the algorithm; set to False if no swaps are made in a pass<\/span>\r\n        swapped = <span style=\"color: #00aaaa;\">False<\/span>\r\n\r\n        <span style=\"color: #aaaaaa; font-style: italic;\"># Last i elements are already in place, so we don't need to check them<\/span>\r\n        <span style=\"color: #0000aa;\">for<\/span> j <span style=\"color: #0000aa;\">in<\/span> <span style=\"color: #00aaaa;\">range<\/span>(<span style=\"color: #009999;\">0<\/span>, n - i - <span style=\"color: #009999;\">1<\/span>):\r\n            <span style=\"color: #0000aa;\">if<\/span> arr[j] &gt; arr[j + <span style=\"color: #009999;\">1<\/span>]:\r\n                <span style=\"color: #aaaaaa; font-style: italic;\"># Swap the elements<\/span>\r\n                arr[j], arr[j + <span style=\"color: #009999;\">1<\/span>] = arr[j + <span style=\"color: #009999;\">1<\/span>], arr[j]\r\n                swapped = <span style=\"color: #00aaaa;\">True<\/span>\r\n\r\n        <span style=\"color: #aaaaaa; font-style: italic;\"># If no two elements were swapped in the inner loop, the array is already sorted<\/span>\r\n        <span style=\"color: #0000aa;\">if<\/span> <span style=\"color: #0000aa;\">not<\/span> swapped:\r\n            <span style=\"color: #0000aa;\">break<\/span>\r\n\r\n<span style=\"color: #aaaaaa; font-style: italic;\"># Example usage:<\/span>\r\nmy_list = [<span style=\"color: #009999;\">64<\/span>, <span style=\"color: #009999;\">34<\/span>, <span style=\"color: #009999;\">25<\/span>, <span style=\"color: #009999;\">12<\/span>, <span style=\"color: #009999;\">22<\/span>, <span style=\"color: #009999;\">11<\/span>, <span style=\"color: #009999;\">90<\/span>]\r\nbubble_sort(my_list)\r\n<span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Sorted array is:\"<\/span>, my_list)\r\n<\/pre>\n<\/div>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Bubble Sort Bubble sort works by iterating through the array repeatedly, comparing adjacent elements and swapping them if they are in the wrong order. The algorithm keeps track of the sorted partition of the array and continues sorting the remaining unsorted partition until the entire array is sorted. This process is repeated until&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Bubble Sort<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bubble Sort | IGCSE Computer Science | Learnlearn.uk<\/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:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bubble Sort | IGCSE Computer Science | Learnlearn.uk\" \/>\n<meta property=\"og:description\" content=\"Introduction to Bubble Sort Bubble sort works by iterating through the array repeatedly, comparing adjacent elements and swapping them if they are in the wrong order. The algorithm keeps track of the sorted partition of the array and continues sorting the remaining unsorted partition until the entire array is sorted. This process is repeated until&hellip;&nbsp;Read More &raquo;Bubble Sort\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"IGCSE Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-04T08:34:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/revise.learnlearn.uk\/django-summernote\/2023-08-27\/177aadce-dec7-40fd-aef2-3033cdecac67.gif\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/\",\"url\":\"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/\",\"name\":\"Bubble Sort | IGCSE Computer Science | Learnlearn.uk\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/#website\"},\"datePublished\":\"2023-11-04T08:32:20+00:00\",\"dateModified\":\"2023-11-04T08:34:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"IGCSE Computer Science Course\",\"item\":\"https:\/\/learnlearn.uk\/igcsecs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bubble Sort\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/#website\",\"url\":\"https:\/\/learnlearn.uk\/igcsecs\/\",\"name\":\"IGCSE Computer Science\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/igcsecs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/#organization\",\"name\":\"IGCSE Computer Science\",\"url\":\"https:\/\/learnlearn.uk\/igcsecs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/igcsecs\/wp-content\/uploads\/sites\/23\/2020\/08\/LearnLearnLogowhitenew.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/igcsecs\/wp-content\/uploads\/sites\/23\/2020\/08\/LearnLearnLogowhitenew.png\",\"width\":710,\"height\":98,\"caption\":\"IGCSE Computer Science\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/igcsecs\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bubble Sort | IGCSE Computer Science | Learnlearn.uk","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:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/","og_locale":"en_GB","og_type":"article","og_title":"Bubble Sort | IGCSE Computer Science | Learnlearn.uk","og_description":"Introduction to Bubble Sort Bubble sort works by iterating through the array repeatedly, comparing adjacent elements and swapping them if they are in the wrong order. The algorithm keeps track of the sorted partition of the array and continues sorting the remaining unsorted partition until the entire array is sorted. This process is repeated until&hellip;&nbsp;Read More &raquo;Bubble Sort","og_url":"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/","og_site_name":"IGCSE Computer Science","article_modified_time":"2023-11-04T08:34:17+00:00","og_image":[{"url":"https:\/\/revise.learnlearn.uk\/django-summernote\/2023-08-27\/177aadce-dec7-40fd-aef2-3033cdecac67.gif"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/","url":"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/","name":"Bubble Sort | IGCSE Computer Science | Learnlearn.uk","isPartOf":{"@id":"https:\/\/learnlearn.uk\/igcsecs\/#website"},"datePublished":"2023-11-04T08:32:20+00:00","dateModified":"2023-11-04T08:34:17+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/igcsecs\/bubble-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"IGCSE Computer Science Course","item":"https:\/\/learnlearn.uk\/igcsecs\/"},{"@type":"ListItem","position":2,"name":"Bubble Sort"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/igcsecs\/#website","url":"https:\/\/learnlearn.uk\/igcsecs\/","name":"IGCSE Computer Science","description":"","publisher":{"@id":"https:\/\/learnlearn.uk\/igcsecs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/igcsecs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/igcsecs\/#organization","name":"IGCSE Computer Science","url":"https:\/\/learnlearn.uk\/igcsecs\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/igcsecs\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/igcsecs\/wp-content\/uploads\/sites\/23\/2020\/08\/LearnLearnLogowhitenew.png","contentUrl":"https:\/\/learnlearn.uk\/igcsecs\/wp-content\/uploads\/sites\/23\/2020\/08\/LearnLearnLogowhitenew.png","width":710,"height":98,"caption":"IGCSE Computer Science"},"image":{"@id":"https:\/\/learnlearn.uk\/igcsecs\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/igcsecs\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Introduction to Bubble Sort Bubble sort works by iterating through the array repeatedly, comparing adjacent elements and swapping them if they are in the wrong order. The algorithm keeps track of the sorted partition of the array and continues sorting the remaining unsorted partition until the entire array is sorted. This process is repeated until&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/pages\/1044"}],"collection":[{"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/comments?post=1044"}],"version-history":[{"count":3,"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/pages\/1044\/revisions"}],"predecessor-version":[{"id":1047,"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/pages\/1044\/revisions\/1047"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/igcsecs\/wp-json\/wp\/v2\/media?parent=1044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}