Skip to content

Commit 1928b15

Browse files
authored
Merge pull request #5 from ms10398/master
Added Implementation of Bubble Sort
2 parents c352f44 + dd23b79 commit 1928b15

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Sorts/bubblesort.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)