{"id":651,"date":"2021-06-23T20:29:32","date_gmt":"2021-06-23T19:29:32","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=651"},"modified":"2021-06-24T09:25:24","modified_gmt":"2021-06-24T08:25:24","slug":"insertion-sort","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/insertion-sort\/","title":{"rendered":"Insertion Sort"},"content":{"rendered":"<p>An insertion sort traverses a list, from the second item to the end of the list.<\/p>\n<p>On each iteration, the item list[n] is placed into it&#8217;s correct position by compared it with each of the earlier items in the list, with values larger than the current\u00a0 being moved up the list, until the <span style=\"font-size: 1rem\">correct location is found.\u00a0<\/span><\/p>\n<h2>General Algorithm<\/h2>\n<pre>FOR i = 1 to length(list)-1 DO\n    SET valueToInsert TO array[i]\n    SET currentIndex TO i\n \n    WHILE (currentIndex&gt; 0) AND (valueToInsert &lt; list[currentIndex-1]) DO\n        SET list[currentIndex] TO list[currentIndex-1]\n        SET currentIndex TO index - 1\n    END WHILE\n \n    SET list[currentIndex] TO valueToInsert \nEND FOR<\/pre>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\n#work through the list from the second item to the last item\r\nfor index in range(1, len(array)):\r\n    # get next value to be put into correct place\r\n    currentValue = array&#x5B;index]\r\n    currentPosition = index\r\n \r\n    #check if not at beginning of list, and previous item is less than value to be placed\r\n    while currentPosition &gt; 0 and array&#x5B;currentPosition - 1] &gt; currentValue:\r\n        #move the lower value up a slot\r\n        array&#x5B;currentPosition] = array&#x5B;currentPosition -1]\r\n        #now for the next previous item in the list\r\n        currentPosition = currentPosition - 1\r\n \r\n    # can put value into correct slot\r\n    array&#x5B;currentPosition] = currentValue\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An insertion sort traverses a list, from the second item to the end of the list. On each iteration, the item list[n] is placed into it&#8217;s correct position by compared it with each of the earlier items in the list, with values larger than the current\u00a0 being moved up the list, until the correct location is found.\u00a0 General Algorithm FOR i = 1 to length(list)-1 DO SET valueToInsert TO array[i]<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/insertion-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-651","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/651","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=651"}],"version-history":[{"count":5,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/651\/revisions"}],"predecessor-version":[{"id":666,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/651\/revisions\/666"}],"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=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}