forked from kokonior/Javascript-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.js
More file actions
30 lines (27 loc) · 715 Bytes
/
BubbleSort.js
File metadata and controls
30 lines (27 loc) · 715 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
28
29
30
const data1 = [4, 2, 5, 1, 3];
const data2 = [3, 2, 1];
const data3 = [9, 1, 2, 6, 5, 3, 7, 8];
const bubbleSort = (data, type = 'asc') => {
let x;
for (let j = 0; j < 3; j++) {
for (let i = 0; i < data.length; i++) {
if (type === 'asc') {
if (data[i] > data[i+1]) {
x = data[i];
data[i] = data[i+1];
data[i+1] = x;
}
} else {
if (data[i] < data[i+1]) {
x = data[i];
data[i] = data[i+1];
data[i+1] = x;
}
}
}
}
return data;
}
console.log(bubbleSort(data1, 'desc')); // 5, 4, 3, 2, 1
console.log(bubbleSort(data2)); // 1, 2, 3
console.log(bubbleSort(data3)); // 1, 2, 3, 5, 6, 7, 8, 9