-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_done.js
More file actions
29 lines (23 loc) · 817 Bytes
/
4_done.js
File metadata and controls
29 lines (23 loc) · 817 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
// 4) Bubblesort (4 punten)
//vb: input="test" output="estt"
//vb: input='3618' output='1368'
//console.log("test".split('').sort().join('')) // estt - 1368
const Bubblesort = (input) => {
// Creeer een array van de input
let array = input.split('')
// Loop over de lengte van de string
for (let i = 0; i < array.length; i++) {
// Loop nog een keer over de lengte van de string maar dan in de omgekeerde richting
for (let x = 0; x < array.length - i; x++) {
console.log(i, x, array[x], array[i])
// Check of de eerste letter groter is dan de volgende letter
if (array[x] > array[x + 1]) {
let temp = array[x];
array[x] = array[x + 1];
array[x + 1] = temp;
}
}
}
return array.join('');
}
console.log(Bubblesort("1z5yx3wvutsr7qponm4lkjihgf2edcba"));