{"id":639,"date":"2021-06-23T15:56:29","date_gmt":"2021-06-23T14:56:29","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=639"},"modified":"2021-06-24T09:24:46","modified_gmt":"2021-06-24T08:24:46","slug":"bubble-sort","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/bubble-sort\/","title":{"rendered":"Bubble Sort"},"content":{"rendered":"<p>A bubble sort repeatedly goes through a list, comparing neighbouring values and swapping them if they are out of order.<\/p>\n<p>After each pass through the list, the highest remaining value has &#8220;bubbled&#8221; through the list to its correct location, so the size of the remaining list to be sorted is reduced by 1.<\/p>\n<p>If no swaps are carried out on a pass, then the sorting can stop.<\/p>\n<h2>General Algorithm<\/h2>\n<pre>DECLARE n INITIALLY length(list)\nDECLARE swapped INITIALLY TRUE\n \nWHILE swapped AND n &gt;= 0\n \n    SET swapped TO False\n    FOR i = 0 to n-2 DO\n        IF list[i] &gt; list[i+1] THEN\n            SET temp TO list[i]\n            SET list[i] TO list[i+1]\n            SET list[i+1] TO temp\n            SET swapped TO TRUE\n        END IF\n    END FOR \n \n    SET n TO n - 1\nEND WHILE<\/pre>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\nlistsize = length(list)\r\nswapped = True\r\n \r\n# as long as previous loop had a swap, and unsorted part of list is not empty\r\nWHILE swapped AND listsize  &gt;= 0:\r\n \r\n    # new iteration, so reset swaps\r\n    swapped = False\r\n    # go through the unsorted part of the list\r\n    for i in range(listsize-1):\r\n        #if current value is bigger than next value\r\n        if list&#x5B;i] &gt; list&#x5B;i+1]:\r\n            #swap the values\r\n            temp = list&#x5B;i]\r\n            list&#x5B;i] = list&#x5B;i+1]\r\n            list&#x5B;i+1] = temp\r\n            swapped = True\r\n \r\n    #the unsorted part of the list is now smaller\r\n    listsize -= 1\r\n<\/pre>\n<h2>Swapping List Values<\/h2>\n<p>Most languages need the use of a temporary variable to swap two values. This is part of the AH course.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ntemp = list&#x5B;i]\r\nlist&#x5B;i] = list&#x5B;i+1]\r\nlist&#x5B;i+1] = temp\r\n<\/pre>\n<p>Python can do this in one command:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nlist&#x5B;i], list&#x5B;i+1] = list&#x5B;i+1], list&#x5B;i]\r\n<\/pre>\n<p>In programmer interviews, one task might be to swap two numbers without the use of a temporary variable. This solution works, but requires several calculations:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nx = 3\r\ny = 5\r\n \r\nx = x + y   # x = 3 + 5 = 8\r\ny = x - y   # y = 8 - 5 = 3\r\nx = x - y   # x = 8 - 3 = 5\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A bubble sort repeatedly goes through a list, comparing neighbouring values and swapping them if they are out of order. After each pass through the list, the highest remaining value has &#8220;bubbled&#8221; through the list to its correct location, so the size of the remaining list to be sorted is reduced by 1. If no swaps are carried out on a pass, then the sorting can stop. General Algorithm DECLARE<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/bubble-sort\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":291,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-639","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=639"}],"version-history":[{"count":10,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/639\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/639\/revisions\/665"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/291"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}