Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 92c9e50

Browse files
authored
Bubble sort in JavaScript (#894)
1 parent 4f0b2f4 commit 92c9e50

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Javascript/Algorithms/bubble-sort

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function bubbleSort(arr){
2+
for(var i = 0; i < arr.length; i++){
3+
// Last i elements are already in place
4+
for(var j = 0; j < ( arr.length - i -1 ); j++){
5+
6+
// Checking if the item at present iteration
7+
// is greater than the next iteration
8+
if(arr[j] > arr[j+1]){
9+
10+
// If the condition is true then swap them
11+
var temp = arr[j]
12+
arr[j] = arr[j + 1]
13+
arr[j+1] = temp
14+
}
15+
}
16+
}
17+
// Print the sorted array
18+
console.log(arr);
19+
}
20+
21+
// Unsorted array
22+
var arr = [234, 43, 55, 63, 5, 6, 235, 547];
23+
24+
// Now pass this array to the bubbleSort() function
25+
bubbleSort(arr);

0 commit comments

Comments
 (0)