-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (30 loc) · 774 Bytes
/
Copy pathindex.js
File metadata and controls
37 lines (30 loc) · 774 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
31
32
33
34
35
36
37
function bubbleSort(array) {
for (let j = 0; j < array.length; j++) {
for (let i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
[ array[i], array[i + 1] ] = [ array[i + 1], array[i] ]
}
}
}
return array;
}
function quickSort (unsortedArray) {
const left = [];
const right = [];
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const pivot = unsortedArray.pop();
unsortedArray.forEach( number => {
if (number < pivot) {
left.push(number);
} else {
right.push(number)
}
})
return [ ...quickSort(left), pivot, ...quickSort(right) ];
}
module.exports = {
bubbleSort,
quickSort
};