Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #25 from gsiradze/master
Browse files Browse the repository at this point in the history
Create bubble-sort.js
Thanks @gsiradze
  • Loading branch information
dhritippaul authored Oct 31, 2020
2 parents cd37802 + d837e29 commit 8a02780
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions SORTING/bubble-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function swap(array, a, b) {
[array[a], array[b]] = [array[b], array[a]];
}

sort(originalArray) {
const array = [...originalArray];
for (let i = 1; i < array.length; i++) {
let swapped = false;
for (let j = 0; j < array.length - i; j++) {
if (array[j + 1] < array[j]) {
swap(array, j + 1, j);
swapped = true;
}
}
if (!swapped) {
return array;
}
}
return array;
}

0 comments on commit 8a02780

Please sign in to comment.