Monday, June 24, 2013

JavaScript Binary Insertion Sort

I ported over the binary insertion sort algorithm from this blog post to JavaScript. The two changes I made were that I used Math.floor to truncate the floats when division occurs and replaced memmove() with a for-loop from one of the previous implementations in the post.
function binaryInsertionSort (a)
{
    var i, m;
    var hi, lo, tmp;
    var n = a.length;

    for (i = 1; i < n; i++) {
        lo = 0, hi = i;
        m = Math.floor(i / 2);

        do {
            if (a[i] > a[m]) {
                lo = m + 1;
            } else if (a[i] < a[m]) {
                hi = m;
            } else
                break;

            m = Math.floor(lo + ((hi - lo) / 2));
        } while (lo < hi);
 
        if (m < i) {
            tmp = a[i];
            for (j = i - 1; j >= m; j--)
                a[j + 1] = a[j];
            a[m] = tmp;
        }
    }
    
    return a;
}

//outputs [11, 40, 42, 66, 73, 90]
binaryInsertionSort([66, 73, 42, 40, 90, 11]); 

Tuesday, June 18, 2013

Load jQuery and jQuery UI into any page

TL;DR You can copy the bookmarklet here. Just drag and drop onto your bookmark bar.

Sometimes I need to load jQuery and jQuery UI on some pages for testing or trying something out. I came up with a bookmarklet that loads the latest jQuery and jQuery UI libraries into the currently viewed page.
(function(){
   var body= document.getElementsByTagName('body')[0];
   var script1 = document.createElement('script');
   script1.type = 'text/javascript';
   script1.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
   var script2 = document.createElement('script');
   script2.type = 'text/javascript';
   script2.src = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js';
   body.appendChild(script1);
   
   var timer = setInterval(function(){
     if (typeof jQuery != 'undefined') { 
        body.appendChild(script2);
        clearTimeout(timer);
     }
   },10);
})()
A problem I ran into is that I couldn't just load jQuery UI into the page right after jQuery because it takes a bit of time for each script to download and jQuery UI is dependent on jQuery being loaded first. The solution I came up with is using a timer that checks every 10ms to see if jQuery has been loaded and then loading jQuery UI. Both are appended to the body (some pages don't have head tags) and the timer cleared to prevent the loop from continuing once the script is done. You could also extend this to add any Javascript you wanted to a page. Here is the code minified:
javascript: (function(){var body= document.getElementsByTagName('body')[0]; var script1 = document.createElement('script'); script1.type= 'text/javascript'; script1.src= 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; var script2 = document.createElement('script'); script2.type= 'text/javascript'; script2.src= 'http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js'; body.appendChild(script1); var timer = setInterval(function(){ if (typeof jQuery != 'undefined') { body.appendChild(script2); clearTimeout(timer); } },10);})()