Skip to content

Commit 85cf854

Browse files
author
Hari Shekhar
committed
Bubble sort with doc explaination
1 parent 40ba377 commit 85cf854

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Bubble Sort/bubbleSort.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const bubbleSort = array => {
2+
const swap = (a, b) => {
3+
var temp = array[a];
4+
array[a] = array[b];
5+
array[b] = temp;
6+
};
7+
for (var outer = 0; outer < array.length; outer++) {
8+
for (var inner = 0; inner < array.length - outer; inner++) {
9+
if (array[inner] > array[inner + 1]) {
10+
swap(inner, inner + 1);
11+
}
12+
}
13+
}
14+
15+
return array;
16+
};
17+
18+
const numbers = [8, 5, 6, 9, 3, 1, 4, 2, 7, 10];
19+
const sortedNumber = bubbleSort(numbers);
20+
console.log(sortedNumber);

Bubble Sort/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Bubble Sort

bubbleSort.js

Whitespace-only changes.

0 commit comments

Comments
 (0)