-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertsort.js
38 lines (36 loc) · 1.01 KB
/
insertsort.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
37
38
function insert(x, xs) { // input num and a list, output a list
if (is_null(xs)) {
return list(x);
} else if (head(xs) > x) {
return pair(x, xs);
} else {
return pair(head(xs), insert(x, tail(xs)));
}
}
function is_sorted(xs) {
function helper(i, xs) {
if (length(xs) === 1) {
return true;
} else if (i === length(xs) -2) {
return list_ref(xs, i) <= list_ref(xs, i+1);
} else if (list_ref(xs, i) <= list_ref(xs, i+1)) {
return helper(i+1, xs);
} else {
return false;
}
}
return helper(0, xs);
}
function insert_sort(xs) { //input list, output a sorted list using insert
if (is_null(xs)) {
return null;
} else if (is_sorted(xs)) {
return xs;
} else if (length(xs) === 1) {
return xs;
} else {
return insert(head(xs), insert_sort(tail(xs)));
}
}
const x = list(5,4,3,2,1, 2, 3, 5, 6);
insert_sort(x);