Difference between revisions of "Algorithm Sorting"

From Hawk Wiki
Jump to: navigation, search
(Created page with "==Insertion sorting== <pre class="brush:javascript"> /////////////////////////////////////////////////////////////////////// // Insertion sorting ////////////////////////////////...")
 
(No difference)

Latest revision as of 23:45, 22 July 2014

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);