Skip to content

Commit 7e6273e

Browse files
committed
bubble sort implemented
1 parent 5614404 commit 7e6273e

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎SortingAlgorithms/bubble_sort.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function bubble_sort(arr){
2+
var i, j, change, temp;
3+
var len = arr.length;
4+
5+
for (i=0; i < len-1; i++){
6+
change = 0;
7+
for (j=0; j < len-1-i; j++){
8+
if (arr[j] > arr[j+1]){
9+
temp = arr[j];
10+
arr[j] = arr[j+1];
11+
arr[j+1] = temp;
12+
change++;
13+
}
14+
}
15+
16+
if(change == 0){
17+
break;
18+
}
19+
}
20+
21+
return arr;
22+
}
23+
24+
console.log(bubble_sort([3, 0, 2, 5, -1, 4, 1]));

0 commit comments

Comments
 (0)