Skip to content

Commit 8cf412e

Browse files
committed
bubblsort.js
1 parent 5faed52 commit 8cf412e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

bubblesort.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
// To sort an array of integers from lowest to highest value.
12
function bubbleSort(array) {
23
for (var i = array.length; i > 0; i--) {
4+
// Notice that j < (length - i)
35
for (var j = 0; j < i; j++) {
6+
// Notice that j < (length - i)
47
if (array[j] > array[j + 1]) {
5-
var temp = array[j];
6-
array[j] = array[j + 1];
8+
// Swap the numbers
9+
var temp = array[j]; // Temporary variable to hold the current number
10+
array[j] = array[j + 1]; // Replace current number with adjacent number
711
array[j + 1] = temp;
812
}
913
}

0 commit comments

Comments
 (0)