forked from GauravWalia19/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble-sort.js
More file actions
27 lines (25 loc) · 703 Bytes
/
bubble-sort.js
File metadata and controls
27 lines (25 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*Bubble Sort is a algorithm to sort a array it
* copares adjacent element and swaps thier position
*/
function bubbleSort(items) {
var length = items.length;
for (var i = (length - 1); i >= 0; i--) {
//Number of passes
for (var j = (length - i); j > 0; j--) {
//Compare the adjacent positions
if (items[j] < items[j - 1]) {
//Swap the numbers
var tmp = items[j];
items[j] = items[j - 1];
items[j - 1] = tmp;
}
}
}
}
//Implementation of bubbleSort
var ar=[5,6,7,8,1,2,12,14];
//Array before Sort
console.log(ar);
bubbleSort(ar);
//Array after sort
console.log(ar);