{"id":3,"date":"2012-02-29T06:46:41","date_gmt":"2012-02-29T06:46:41","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3"},"modified":"2012-02-29T06:46:41","modified_gmt":"2012-02-29T06:46:41","slug":"javascript-quicksort-implementation-with-dynamic-comparator","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/javascript-quicksort-implementation-with-dynamic-comparator\/","title":{"rendered":"Javascript Quicksort implementation with dynamic comparator."},"content":{"rendered":"<p>[javascript]<br \/>\nArray.prototype.swap=function(a, b)<br \/>\n{<br \/>\n    var tmp=this[a];<br \/>\n    this[a]=this[b];<br \/>\n    this[b]=tmp;<br \/>\n}<\/p>\n<p>function quickSort(array,comparator) {<br \/>\n    qsort(array,0,array.length,comparator);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * NOTE: the comparator is a dynamic function you will define<br \/>\n   like so comparator(a,b) {<br \/>\n     if (a &gt; b) return 1;<br \/>\n     else if (a &lt; b) return -1;<br \/>\n     else {<br \/>\n        return 0;<br \/>\n     }<br \/>\n   }<br \/>\n * it is up to you to determine the comparison logic between the elements.<br \/>\n * it&#8217;s particularly useful if you&#8217;re comparing objects and different properties<br \/>\n * inside them.<br \/>\n *\/<br \/>\nfunction qsort(array, begin, end, comparator)<br \/>\n{<br \/>\n    if(end-1&gt;begin) {<br \/>\n\tvar pivot=begin+Math.floor(Math.random()*(end-begin));<\/p>\n<p>\tpivot=partition(array, begin, end, pivot, comparator);<\/p>\n<p>\tqsort(array, begin, pivot, comparator);<br \/>\n\tqsort(array, pivot+1, end, comparator);<br \/>\n    }<br \/>\n}<\/p>\n<p>function partition(array, begin, end, pivot, comparator) {<br \/>\n    var piv=array[pivot];<br \/>\n    array.swap(pivot, end-1);<br \/>\n    var store=begin;<br \/>\n    var ix;<br \/>\n    for(ix=begin; ix&lt;end-1; ++ix) {<br \/>\n        if (piv!=undefined &amp;&amp; comparator(piv, array[ix]) &lt; 0) {<br \/>\n\t    array.swap(store, ix);<br \/>\n\t    ++store;<br \/>\n\t}<br \/>\n    }<br \/>\n    array.swap(end-1, store);<\/p>\n<p>    return store;<br \/>\n}<br \/>\n[\/javascript]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[javascript] Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b]; this[b]=tmp; } function quickSort(array,comparator) { qsort(array,0,array.length,comparator); } \/** * NOTE: the comparator is a dynamic function you will define like so comparator(a,b) { if (a &gt; b) return 1; else if (a &lt; b) return -1; else { return 0; } } * it is up to you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[],"class_list":["post-3","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-3","jetpack-related-posts":[{"id":2121,"url":"https:\/\/www.gubatron.com\/blog\/playing-with-basics-of-html5-canvas\/","url_meta":{"origin":3,"position":0},"title":"Playing with basics of HTML5 Canvas","author":"gubatron","date":"December 18, 2010","format":false,"excerpt":"Here's a snippet of something I did tonight to play a bit with the Canvas and 2d Graphics context objects in javascript. [html] <html> <head> <title>Playing with Canvas<\/title> <\/head> <body style=\"padding:0 0; margin: 0 0; background:black\"> <canvas id=\"myCanvas\" width=\"1024\" height=\"1024\" ><\/canvas> <script type=\"text\/javascript\"> \/\/convert to CSS friendly Hex String function\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":799,"url":"https:\/\/www.gubatron.com\/blog\/function-callbacks-in-c\/","url_meta":{"origin":3,"position":1},"title":"Function callbacks in C","author":"gubatron","date":"May 2, 2008","format":false,"excerpt":"Ever since I started programming in Javascript, and doing asynchronous function calls, I've found myself to be addicted to passing functions as parameters. I do it a lot in python and php, it's very easy to do this on all these dynamic typed languages. I never had this concept of\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2233,"url":"https:\/\/www.gubatron.com\/blog\/twitterautolinks-jquery-extension-to-replace-twitter-names-for-links-to-their-respective-pages\/","url_meta":{"origin":3,"position":2},"title":"twitterAutoLinks: jQuery extension to replace twitter @names for links to their respective pages.","author":"gubatron","date":"August 2, 2011","format":false,"excerpt":"Save the following on a .js file, preferably named jquery.twitterAutoLinks.js [javascript] \/** Auto replace all Twitter nicknames for links *\/ $.fn.twitterAutoLinks = function() { return this.each(function() { var html = $(this).html(); $(this).html(html.replace(\/B@([w-]+)\/gm, '<a href=\"http:\/\/twitter.com\/$1\" target=\"_blank\">@$1<\/a>')); }); }; [\/javascript] Usage After importing the jquery.twitterAutoLinks.js file (make sure it's after you've imported\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2632,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-n-random-elements-from-a-list\/","url_meta":{"origin":3,"position":3},"title":"javascript: Get N random elements from a List","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Walks linearly through the list to find an element. * returns true if it's found. *\/ function elementIn(collection, element) { for (var i=0; i < collection.length; i++) { if (collection[i]==element) { return true; } } return false; } \/** * Returns a new list of n random\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2671,"url":"https:\/\/www.gubatron.com\/blog\/how-to-shuffle-a-listarray-in-javascript\/","url_meta":{"origin":3,"position":4},"title":"How to shuffle a List (or Array) in Javascript","author":"gubatron","date":"February 24, 2012","format":false,"excerpt":"[javascript] \/** * Returns number starting from offset up to n-1 *\/ function getRandomWithOffset(n,offset) { return Math.floor(Math.random()*n+offset); } \/** * Returns random integer from 0 to n-1 *\/ function getRandom(n) { return getRandomWithOffset(n,0); } \/** Fisher\u2013Yates shuffle algorithm O(n) *\/ function shuffleList(list) { for (var i=list.length-1; i>0; i--) { var\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2637,"url":"https:\/\/www.gubatron.com\/blog\/javascript-capitalize-text-like-in-this-title\/","url_meta":{"origin":3,"position":5},"title":"Javascript: Capitalize Text Like In This Title","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"On this one, we'll show of the dynamic nature of javascript and we're going to make all strings have a new method called \"capitalize()\". So you can do stuff like this: [javascript] console.log(\"this text should look nicer now\".capitalize()); >> This Text Should Look Nicer Now [\/javascript] Here's the magic code\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3"}],"version-history":[{"count":2,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3\/revisions"}],"predecessor-version":[{"id":2687,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3\/revisions\/2687"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}