-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.js
More file actions
165 lines (147 loc) · 4.88 KB
/
Copy pathalgorithms.js
File metadata and controls
165 lines (147 loc) · 4.88 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function insertionSort(array, descendingOrder = false) {
for (let i = 1; i < array.length; i++) {
let key = array[i];
let j = i - 1;
if (descendingOrder) {
while (j >= 0 && array[j] < key) {
array[j + 1] = array[j];
j = j - 1
}
array[j + 1] = key
} else {
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1
}
array[j + 1] = key
}
}
return array;
}
function selectionSort(array, descendingOrder = false) {
for (let i = 0; i < array.length - 1; i++) {
let m = i;
let j = i + 1;
if (descendingOrder) {
while (j < array.length) {
if (array[j] > array[m]) {
m = j
}
j = j + 1
}
} else {
while (j < array.length) {
if (array[j] < array[m]) {
m = j
}
j = j + 1
}
}
let buffer = array[i];
array[i] = array[m];
array[m] = buffer;
}
return array;
}
function mergeSort(array, descendingOrder = false) {
if (array.length > 1) {
let mid = array.length / 2;
let left_array = array.slice(0, mid);
let right_array = array.slice(mid);
mergeSort(left_array);
mergeSort(right_array);
let i = 0;
let j = 0;
let k = 0;
while (i < left_array.length && j < right_array.length) {
if (left_array[i] < right_array[j]) {
array[k] = left_array[i];
i += 1;
} else {
array[k] = right_array[j];
j += 1;
}
k += 1;
}
while (i < left_array.length) {
array[k] = left_array[i];
i += 1;
k += 1;
}
while (j < right_array.length) {
array[k] = right_array[j];
j += 1;
k += 1;
}
}
if (descendingOrder) {
return array.reverse();
} else {
return array
}
}
function bubbleSort(array, descendingOrder = false) {
for (let i = 0; i < array.length - 1; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (descendingOrder) {
if (array[j] < array[j + 1]) {
let buffer = array[j];
array[j] = array[j + 1];
array[j + 1] = buffer;
}
} else {
if (array[j] > array[j + 1]) {
let buffer = array[j];
array[j] = array[j + 1];
array[j + 1] = buffer;
}
}
}
}
return array;
}
function findMaxSubarray(array) {
let best_sum = 0;
let current_sum = 0;
for (let i = 0; i < array.length; i += 1) {
current_sum = Math.max(0, current_sum + array[i]);
best_sum = Math.max(best_sum, current_sum);
}
return best_sum;
}
function test() {
console.log("Testing insertion sort");
console.log("______________________");
console.log(insertionSort([5, 3, 4, 2, 1, 2, 3, 5, 2], true));
console.log(insertionSort([1, 2, 4, 2, 2, 1, 5, 6, 6, 9]));
console.log(insertionSort(["a", "b", "d", "a", "h", "a"]));
console.log(insertionSort(["a", "b", "d", "a", "h", "a"], true));
console.log("______________________");
console.log("Testing selection sort");
console.log("______________________");
console.log(selectionSort([5, 3, 4, 2, 1, 2, 3, 5, 2], true));
console.log(selectionSort([1, 2, 4, 2, 2, 1, 5, 6, 6, 9]));
console.log(selectionSort(["a", "b", "d", "a", "h", "a"]));
console.log(selectionSort(["a", "b", "d", "a", "h", "a"]), true);
console.log("______________________");
console.log("Testing merge sort");
console.log("______________________");
console.log(mergeSort([5, 3, 4, 2, 1, 2, 3, 5, 2], true));
console.log(mergeSort([1, 2, 4, 2, 2, 1, 5, 6, 6, 9]));
console.log(mergeSort(["a", "b", "d", "a", "h", "a"]));
console.log(mergeSort(["a", "b", "d", "a", "h", "a"], true));
console.log("______________________");
console.log("Testing bubble sort");
console.log("______________________");
console.log(bubbleSort([5, 3, 4, 2, 1, 2, 3, 5, 2], true));
console.log(bubbleSort([1, 2, 4, 2, 2, 1, 5, 6, 6, 9]));
console.log(bubbleSort(["a", "b", "d", "a", "h", "a"]));
console.log(bubbleSort(["a", "b", "d", "a", "h", "a"], true));
console.log("______________________");
console.log("Testing find maximum subarray");
console.log("______________________");
console.log(findMaxSubarray([5, 3, -4, 2, 1, -2, 3, 5, 2]));
console.log(findMaxSubarray([1, 2, -4, 2, 2, -1, 5, 6, 6, 9]));
console.log("______________________");
}
test();