Skip to content

Commit 3f5a1ff

Browse files
authored
Merge pull request vJechsmayr#22 from skyridertk/bubble-sort
Implement bubble sort algorithm
2 parents 0ac53fb + 4e0b5b4 commit 3f5a1ff

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎bubbleSort.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function bubbleSort(temp_arr){
2+
let arr = [...temp_arr];
3+
4+
//traverse through all the elements
5+
for(let i=0; i<arr.length; i++){
6+
7+
for(let j=0; j<arr.length; j++){
8+
if(arr[j] > arr[j+1]){
9+
10+
//swap if element found is greater
11+
[arr[j], arr[j+1]] = [arr[j+1], arr[j]];
12+
}
13+
}
14+
}
15+
16+
return arr;
17+
}
18+
19+
//testing
20+
let values = [5,6,3,34];
21+
22+
console.log(bubbleSort(values));

0 commit comments

Comments
 (0)