Posted By

eryoav on 09/19/11


Tagged

sort


Versions (?)

bubble sort


 / Published in: JavaScript
 

URL: http://jsfiddle.net/zapkb/

bubble sort

  1. var arr = [8, 2, 6, 1, 5, 4];
  2.  
  3. for (var i = 0; i < arr.length - 1; i++){
  4. for (var j = 0; j < arr.length -1; j++) {
  5. if (arr[j] > arr[j + 1]) {
  6.  
  7. var tempVar1 = arr[j];
  8. var tempVar2 = arr[j + 1];
  9.  
  10. //swap places
  11. arr[j] = tempVar2;
  12. arr[j + 1] = tempVar1;
  13. }
  14. }
  15. }
  16. alert(arr.join(","))

Report this snippet  

You need to login to post a comment.