{"id":308,"date":"2011-03-07T13:14:23","date_gmt":"2011-03-07T19:14:23","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=308"},"modified":"2012-11-05T12:59:06","modified_gmt":"2012-11-05T18:59:06","slug":"code-of-the-day-javascript-insertion-sort","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/03\/code-of-the-day-javascript-insertion-sort\/","title":{"rendered":"Code of the day &#8211; Javascript Insertion Sort"},"content":{"rendered":"<p><b>Insertion Sort. <\/b><br \/>\nBest: O(n), Average: O(n<sup>2<\/sup>), Worst: O(n<sup>2<\/sup>)<br \/>\nInsertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time.<br \/>\nVisit <a href=\"http:\/\/en.wikipedia.org\/wiki\/Insertion_sort\">Wikipedia<\/a> for more information.<\/p>\n<h2>Code<\/h2>\n<pre lang=\"javascript\" line='1'>\r\n\/\/Programmer: Larry Battle Mar 9, 2011\r\n\/\/Purpose: Insertion sort implemented in Javascript.\r\nvar insertionSort = function (arr) {\r\n    var len = arr.length, i = -1, j, tmp;\r\n\r\n    while (len--) {\r\n        tmp = arr[++i];\r\n        j = i;\r\n        while (j-- && arr[j] > tmp) {\r\n            arr[j + 1] = arr[j];\r\n        }\r\n        arr[j + 1] = tmp;\r\n    }\r\n};\r\n<\/pre>\n<h2>Usage<\/h2>\n<pre lang=\"javascript\" line='1'>\r\ninsertionSort( [5,4,3,2,1] ); \/\/returns [1,2,3,4,5];\r\ninsertionSort( [ \"bear\", \"dog\", \"cat\" ] ) \/\/ returns [\"bear\", \"cat\", \"dog\"];\r\n<\/pre>\n<h2>Demo<\/h2>\n<p><a href=\"http:\/\/jsbin.com\/usavi3\/3\">Jsbin.com<\/a><br \/>\n<a href=\"http:\/\/bateru.com\/news\/wp-content\/uploads\/2011\/03\/insertionSortInJavascript.html\">bateru.com\/uploads<\/a><\/p>\n<p>Here&#8217;s a great short video tutorial to help those clueless about insertion sort.<br \/>\n<iframe loading=\"lazy\" title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http:\/\/www.youtube.com\/embed\/c4BRHC7kTaQ\" frameborder=\"0\" allowfullscreen><\/iframe><br \/>\n<br \/>\n<b>Update:<\/b><\/p>\n<pre lang=\"javascript\">\r\n\/**\r\n* Sorts an array using insertion sort.\r\n* \r\n* @param {Array} - arr\r\n* @param {Boolean} areNumbers indicates whether the array elements should be cast as numbers.\r\n* @return {Array} the sorted array.\r\n*\/\r\nvar insertionSort = function (arr, areNumbers) {\r\n\tarr = arr.concat();\r\n\tvar len = arr.length,\r\n\ti = -1,\r\n\tj,\r\n\ttmp;\r\n\twhile (len--) {\r\n\t\ttmp = arr[++i];\r\n\t\tj = i;\r\n\t\twhile (j-- && areNumbers ? (+arr[j] > +tmp) : (arr[j] > tmp) ) {\r\n\t\t\tarr[j + 1] = arr[j];\r\n\t\t}\r\n\t\tarr[j + 1] = tmp;\r\n\t}\r\n\treturn arr;\r\n};\r\n<\/pre>\n<pre lang=\"javascript\">\r\n\/\/ Treat input as array of numbers\r\nInput: insertionSort([\"1\",\"2\",\"3\",\"4\",\"4\",\"40\",\"20\",\"21\"], true);\r\nOutput: [\"1\", \"2\", \"3\", \"4\", \"4\", \"20\", \"21\", \"40\"]\r\n\r\n\/\/ Treat input as array of strings\r\nInput: insertionSort([\"1\",\"2\",\"3\",\"4\",\"4\",\"40\",\"20\",\"21\"]);\r\nOutput: [\"1\", \"2\", \"20\", \"21\", \"3\", \"4\", \"4\", \"40\"]\r\n<\/pre>\n<p><OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http:\/\/fpdownload.macromedia.com\/get\/flashplayer\/current\/swflash.cab\" id=\"Player_9163ab4e-d67d-4b70-ac6c-55093b405257\"  WIDTH=\"336px\" HEIGHT=\"280px\"> <PARAM NAME=\"movie\" VALUE=\"http:\/\/ws.amazon.com\/widgets\/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fbaterucom-20%2F8009%2F9163ab4e-d67d-4b70-ac6c-55093b405257&#038;Operation=GetDisplayTemplate\"><PARAM NAME=\"quality\" VALUE=\"high\"><PARAM NAME=\"bgcolor\" VALUE=\"#FFFFFF\"><PARAM NAME=\"allowscriptaccess\" VALUE=\"always\"><embed src=\"http:\/\/ws.amazon.com\/widgets\/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fbaterucom-20%2F8009%2F9163ab4e-d67d-4b70-ac6c-55093b405257&#038;Operation=GetDisplayTemplate\" id=\"Player_9163ab4e-d67d-4b70-ac6c-55093b405257\" quality=\"high\" bgcolor=\"#ffffff\" name=\"Player_9163ab4e-d67d-4b70-ac6c-55093b405257\" allowscriptaccess=\"always\"  type=\"application\/x-shockwave-flash\" align=\"middle\" height=\"280px\" width=\"336px\"><\/embed><\/OBJECT> <NOSCRIPT><A HREF=\"http:\/\/ws.amazon.com\/widgets\/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fbaterucom-20%2F8009%2F9163ab4e-d67d-4b70-ac6c-55093b405257&#038;Operation=NoScript\">Amazon.com Widgets<\/A><\/NOSCRIPT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Insertion Sort. Best: O(n), Average: O(n2), Worst: O(n2) Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. Visit Wikipedia for more information. Code \/\/Programmer: Larry Battle Mar 9, 2011 \/\/Purpose: Insertion sort implemented in Javascript. var insertionSort = function (arr) &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/03\/code-of-the-day-javascript-insertion-sort\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day &#8211; Javascript Insertion Sort<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[30,28,164,29],"class_list":["post-308","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials","tag-code-of-the-day","tag-insertion-sort","tag-javascript","tag-jsbin"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=308"}],"version-history":[{"count":16,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"predecessor-version":[{"id":317,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/308\/revisions\/317"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}