-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
28 lines (25 loc) · 680 Bytes
/
sort.js
File metadata and controls
28 lines (25 loc) · 680 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
const testArr = [5,8,3,6,2,1,91,231,4,634,23,6]
// quick sort
function quickSort(arr) {
if (arr.length < 2) return arr
const less = [], greater = []
const base = arr[0]
for (let i = 1; i < arr.length; i++) {
arr[i] <= base ? less.push(arr[i]) : greater.push(arr[i])
}
return [...quickSort(less), base, ...quickSort(greater)]
}
console.log(quickSort(testArr))
function bubbleSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
return arr
}
console.log(bubbleSort(testArr))