Javascript Array bubbleSort(array)

Description

Javascript Array bubbleSort(array)


Array.prototype.bubbleSort = function (array) {
  var status = true;
  while (status) {
    for (var i = 0; i < array.length-1; i++) {
    status = false;/*  w w  w . java  2  s . c o  m*/
      for (var j = i + 1; j < array.length; j++) {
        if (array[i] > array[j]) {
          var temp = array[i];
          array[i] = array[j];
          array[j] = temp;
          status = true;
        }
      }
    }
  }
  console.log(array);
};

Array.prototype.bubbleSort([5,3,7,4,0,1]);



PreviousNext

Related