-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathscript.js
36 lines (29 loc) · 810 Bytes
/
script.js
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
31
32
33
34
35
36
const sortButton = document.getElementById('sort');
const output = document.getElementById('output');
sortButton.addEventListener('click', () => {
const text = document.getElementById('input').value;
const list = text.replace(/ /g, '').split(',');
const sortedList = bubbleSort(list);
output.innerHTML = '';
sortedList.forEach((num, index) => {
const span = document.createElement('span');
span.innerText = `${num} `;
output.appendChild(span);
});
});
function swap(list, i, j) {
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
function bubbleSort(list) {
let length = list.length;
for (let i = 0; i < length; i++) {
for (let j = 0; j < length - i; j++) {
if (list[j] > list[j + 1]) {
swap(list, j, j + 1);
}
}
}
return list;
}