JavaScript BubbleSort

Posted by Alyn on Stack Overflow See other posts from Stack Overflow or by Alyn
Published on 2010-05-20T15:38:02Z Indexed on 2010/05/20 15:40 UTC
Read the original article Hit count: 198

Filed under:
|

Hi,

Have a bubblesort routine similar the this. I need to make it more efficient by stopping the loop when the array is sorted or if the array is already sorted.

function sortNumbers(listbox) {
  var x, y, holder;
  // The Bubble Sort method.
  for(x = 0; x < ranarray.length; x++) {
    for(y = 0; y < (ranarray.length-1); y++) {
      if(ranarray[y] > ranarray[y+1]) {
        holder = ranarray[y+1];
        ranarray[y+1] = ranarray[y];
        ranarray[y] = holder;
      }
    }
  }

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about bubble-sort