Skip to content

Commit

Permalink
Complete bubbleSort.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Dec 27, 2014
1 parent eacfd89 commit e879efc
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bubbleSort/bubbleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,22 @@ var i;
// Feel free to add helper functions if needed.

var bubbleSort = function(array) {
var length = array.length;
var i, j, temp, noSwap;

for (j = 0; j < length; j += 1) {
noSwap = true;
for (i = 0; i < length - j - 1; i += 1) {
if (array[i] > array[i + 1]) {
// swap
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
noSwap = false;
}
}
if (noSwap) { break; }
}

return array;
};

0 comments on commit e879efc

Please sign in to comment.