Skip to content

Commit 37d4ce9

Browse files
committed
Changed inner loop on bubble sort implementations so that they do not needlessly compare the last item to an undefined value when i is 0
1 parent 0183240 commit 37d4ce9

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

algorithms/sorting/bubble-sort/bubble-sort-2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function bubbleSort(items){
4747
i, j;
4848

4949
for (i=len-1; i >= 0; i--){
50-
for (j=len-i; j >= 0; j--){
50+
for (j=len-i-1; j >= 0; j--){
5151
if (items[j] < items[j-1]){
5252
swap(items, j, j-1);
5353
}

algorithms/sorting/bubble-sort/bubble-sort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function bubbleSort(items){
4747
i, j, stop;
4848

4949
for (i=0; i < len; i++){
50-
for (j=0, stop=len-i; j < stop; j++){
50+
for (j=0, stop=len-i-1; j < stop; j++){
5151
if (items[j] > items[j+1]){
5252
swap(items, j, j+1);
5353
}

0 commit comments

Comments
 (0)