{"id":1139,"date":"2020-11-05T21:07:04","date_gmt":"2020-11-05T21:07:04","guid":{"rendered":"http:\/\/learnlearn.uk\/alevelcs\/?page_id=1139"},"modified":"2021-03-26T06:47:38","modified_gmt":"2021-03-26T06:47:38","slug":"bubble-sort","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/","title":{"rendered":"Bubble Sort"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Introduction<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Bubble Sort &#8211; O(n\u00b2)<\/h3>\n<p>Bubble sort is a very easy to code Sorting Algorithm that sorts items in an array into some order.<\/p>\n<p>It could be used for :<\/p>\n<ul>\n<li>Numerical Order<\/li>\n<li>Alphabetical Order<\/li>\n<li>Time Order<\/li>\n<\/ul>\n<p>Although it is simple to code it is incredibly slow, especially for larger arrays, and so is hardly ever used. You can tell it is really efficient by looking at the <strong>n squared<\/strong> part of the Big 0 value above, it shows an exponential relationship!!!<\/p>\n<ul>\n<li>10 items &#8211; n\u00b2 = 100<\/li>\n<li>100 items &#8211; n\u00b2 = 10,000<\/li>\n<li>1000\u00a0items &#8211; n\u00b2 = 1,000,000<\/li>\n<\/ul>\n<h3>How does Bubble Sort work?<\/h3>\n<p><span style=\"color: #0000ff;\"><em>AS &amp; A Level \u2013 You are required to know how it works and be able to write Code \/ Pseudocode for the algorithm<\/em><\/span><\/p>\n<p>The bubble sort algorithm works by sorting through the array in pairs.<\/p>\n<ol>\n<li>It starts at the left of the array and inspects the first two items.<\/li>\n<li>If the items are in the wrong order they are swapped around.<\/li>\n<li>The algorithm then carries on with the next pair along and so forth.<\/li>\n<li>When it reaches the end of the array it goes back to the beginning of the array and starts again.<\/li>\n<li>If it completes a full pass of the array without swapping any items it knows that the array is sorted.<\/li>\n<\/ol>\n\n<\/div><h2 class=\"tabtitle\">Demonstration<\/h2>\n<div class=\"tabcontent\">\n\n<h4>Bubble Sort Demonstration (Using Scratch)<br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/scratch.mit.edu\/projects\/444092744\/embed\" width=\"900\" height=\"600\" frameborder=\"0\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\" data-mce-fragment=\"1\"><\/iframe><\/h4>\n<p><a href=\"https:\/\/scratch.mit.edu\/projects\/444092744\/\">You can find the Scratch project page here<\/a><\/p>\n\n<\/div><h2 class=\"tabtitle\">Python Tutorial<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Bubble Sort<\/h3>\n<p><strong>Bubble Sort Python Tutorial<\/strong><\/p>\n<div class=\"nv-iframe-embed\">\n<div class=\"container-lazyload preview-lazyload container-youtube js-lazyload--not-loaded\"><a href=\"https:\/\/www.youtube.com\/watch?v=sRioelAlIaY\" class=\"lazy-load-youtube preview-lazyload preview-youtube\" data-video-title=\"Bubble Sort Python tutorial for beginners\" title=\"Play video &quot;Bubble Sort Python tutorial for beginners&quot;\">https:\/\/www.youtube.com\/watch?v=sRioelAlIaY<\/a><noscript>Video can&#8217;t be loaded because JavaScript is disabled: <a href=\"https:\/\/www.youtube.com\/watch?v=sRioelAlIaY\" title=\"Bubble Sort Python tutorial for beginners\">Bubble Sort Python tutorial for beginners (https:\/\/www.youtube.com\/watch?v=sRioelAlIaY)<\/a><\/noscript><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Python Code<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Python Code<\/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: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">time<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0e84b5; font-weight: bold;\">random<\/span>\r\n\r\nl <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">7<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">6<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">2<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">3<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">44<\/span>]\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">bubble_sort<\/span>(l):\r\n    <span style=\"color: #008800; font-weight: bold;\">while<\/span> <span style=\"color: #007020;\">True<\/span>:\r\n        swapped <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">False<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">for<\/span> index <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #007020;\">range<\/span>(<span style=\"color: #007020;\">len<\/span>(l)<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>):\r\n            <span style=\"color: #008800; font-weight: bold;\">if<\/span> l[index] <span style=\"color: #333333;\">&gt;<\/span> l[index<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>]:\r\n               l[index], l[index<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>] <span style=\"color: #333333;\">=<\/span> l[index<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>], l[index]\r\n               swapped <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">True<\/span> \r\n            \r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span> swapped <span style=\"color: #333333;\">==<\/span> <span style=\"color: #007020;\">False<\/span>:\r\n            <span style=\"color: #008800; font-weight: bold;\">return<\/span>\r\n\r\nbubble_sort(l)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span>(l)\r\n<\/pre>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Pseudocode<\/h2>\n<div class=\"tabcontent\">\n\n<h3>\u00a0PseudoCode<\/h3>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bubble Sort &#8211; O(n\u00b2) Bubble sort is a very easy to code Sorting Algorithm that sorts items in an array into some order. It could be used for : Numerical Order Alphabetical Order Time Order Although it is simple to code it is incredibly slow, especially for larger arrays, and so is hardly ever used.&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/alevelcs\/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":"","neve_meta_content_width":70,"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 - A Level Computer Science<\/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\/alevelcs\/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 - A Level Computer Science\" \/>\n<meta property=\"og:description\" content=\"Bubble Sort &#8211; O(n\u00b2) Bubble sort is a very easy to code Sorting Algorithm that sorts items in an array into some order. It could be used for : Numerical Order Alphabetical Order Time Order Although it is simple to code it is incredibly slow, especially for larger arrays, and so is hardly ever used.&hellip;&nbsp;Read More &raquo;Bubble Sort\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"A Level Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2021-03-26T06:47:38+00:00\" \/>\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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/\",\"name\":\"Bubble Sort - A Level Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#website\"},\"datePublished\":\"2020-11-05T21:07:04+00:00\",\"dateModified\":\"2021-03-26T06:47:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"A Level Computer Science Home\",\"item\":\"https:\/\/learnlearn.uk\/alevelcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bubble Sort\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#website\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/\",\"name\":\"A Level Computer Science\",\"description\":\"CIE Specification\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/alevelcs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#organization\",\"name\":\"A Level Computer Science\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"A Level Computer Science\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bubble Sort - A Level Computer Science","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\/alevelcs\/bubble-sort\/","og_locale":"en_GB","og_type":"article","og_title":"Bubble Sort - A Level Computer Science","og_description":"Bubble Sort &#8211; O(n\u00b2) Bubble sort is a very easy to code Sorting Algorithm that sorts items in an array into some order. It could be used for : Numerical Order Alphabetical Order Time Order Although it is simple to code it is incredibly slow, especially for larger arrays, and so is hardly ever used.&hellip;&nbsp;Read More &raquo;Bubble Sort","og_url":"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/","og_site_name":"A Level Computer Science","article_modified_time":"2021-03-26T06:47:38+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/","url":"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/","name":"Bubble Sort - A Level Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#website"},"datePublished":"2020-11-05T21:07:04+00:00","dateModified":"2021-03-26T06:47:38+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/alevelcs\/bubble-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"A Level Computer Science Home","item":"https:\/\/learnlearn.uk\/alevelcs\/"},{"@type":"ListItem","position":2,"name":"Bubble Sort"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/alevelcs\/#website","url":"https:\/\/learnlearn.uk\/alevelcs\/","name":"A Level Computer Science","description":"CIE Specification","publisher":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/alevelcs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/alevelcs\/#organization","name":"A Level Computer Science","url":"https:\/\/learnlearn.uk\/alevelcs\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"A Level Computer Science"},"image":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/alevelcs\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Bubble Sort &#8211; O(n\u00b2) Bubble sort is a very easy to code Sorting Algorithm that sorts items in an array into some order. It could be used for : Numerical Order Alphabetical Order Time Order Although it is simple to code it is incredibly slow, especially for larger arrays, and so is hardly ever used.&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/1139"}],"collection":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/comments?post=1139"}],"version-history":[{"count":6,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/1139\/revisions"}],"predecessor-version":[{"id":1862,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/1139\/revisions\/1862"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/media?parent=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}