Posted By

eryoav on 03/03/12


Tagged

algorithm


Versions (?)

insertion sort


 / Published in: JavaScript
 

insertion sort

  1. var collection = [2, 5, 1];
  2. for (var j=1; j<collection.length;j++) {
  3. var currElem = collection[j];
  4. var i = j-1;
  5.  
  6. while(i >= 0 && collection[i] > currElem) {
  7. collection[i+1] = collection[i];
  8. i = i - 1;
  9. }
  10. collection[i+1] = currElem;
  11. }

Report this snippet  

You need to login to post a comment.