Skip to content

Instantly share code, notes, and snippets.

@wintercn
Created September 30, 2013 03:19
Show Gist options
  • Select an option

  • Save wintercn/6758985 to your computer and use it in GitHub Desktop.

Select an option

Save wintercn/6758985 to your computer and use it in GitHub Desktop.

Revisions

  1. wintercn created this gist Sep 30, 2013.
    22 changes: 22 additions & 0 deletions adhocSort.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    function shuffle(array){
    for(var i = array.length-1; i > 0; i--) {
    var rnd = Math.floor(Math.random() * (i+1));
    var tmp = array[i];
    array[i] = array[rnd];
    array[rnd] = tmp;
    }
    }
    function isInOrder(array) {
    for(var i = 0; i < array.length-1; i++)
    if(array[i]>array[i+1])
    return false;
    return true;
    }

    function adhocSort(array){
    while(!isInOrder(array))
    shuffle(array);
    return array;
    }

    adhocSort([3,2,1,6,7]);