Algorithm Sorting

From Hawk Wiki
Revision as of 23:45, 22 July 2014 by Hall (Talk | contribs) (Created page with "==Insertion sorting== <pre class="brush:javascript"> /////////////////////////////////////////////////////////////////////// // Insertion sorting ////////////////////////////////...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Insertion sorting

///////////////////////////////////////////////////////////////////////
// Insertion sorting
///////////////////////////////////////////////////////////////////////
var arr =  [5,2,1,3,4,6,5];
for(var j = 1; j < arr.length ; j++) {
    var i = j - 1;
    var current_val = arr[j];
    while(i >= 0 && arr[i] > current_val) {
        arr[i+1] = arr[i];
        arr[i] = current_val;
        i--;
    }
}
console.log(arr);