Skip to content

Commit 3736d0b

Browse files
ms10398the-cybersapien
authored andcommitted
Create bubble-sort.js (#15)
* Create bubble-sort.js * Add: Contributors.md (#23) * Added Contributors.md * Fixed Readme.md
1 parent f7bdd81 commit 3736d0b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Javascript/Sort/bubble-sort.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*Bubble Sort is a algorithm to sort a array it
2+
* copares adjacent element and swaps thier position
3+
*/
4+
function bubbleSort(items) {
5+
var length = items.length;
6+
for (var i = (length - 1); i >= 0; i--) {
7+
//Number of passes
8+
for (var j = (length - i); j > 0; j--) {
9+
//Compare the adjacent positions
10+
if (items[j] < items[j - 1]) {
11+
//Swap the numbers
12+
var tmp = items[j];
13+
items[j] = items[j - 1];
14+
items[j - 1] = tmp;
15+
}
16+
}
17+
}
18+
}
19+
20+
//Implementation of bubbleSort
21+
22+
var ar=[5,6,7,8,1,2,12,14];
23+
//Array before Sort
24+
console.log(ar);
25+
bubbleSort(ar);
26+
//Array after sort
27+
console.log(ar);

0 commit comments

Comments
 (0)