-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsalgorithms.js
More file actions
259 lines (218 loc) · 6.3 KB
/
jsalgorithms.js
File metadata and controls
259 lines (218 loc) · 6.3 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// *** Algorithms: Bubble Sort ***
function bubbleSort(array) {
for (let i = array.length - 1; i > 0; i--) {
for (let j = 0; j < i; j++) {
if (array[j] > array[j+1]) {
let temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
}
}
}
return array
}
console.log(bubbleSort(['a', 'c', 'z', 'g', 's', 'i']))
console.log(bubbleSort([22, 1, 3, 8, 4, 2]))
// *** Algorithms: Selection Sort ***
function selectionSort(array) {
let min
for (let i = 0; i < array.length - 1; i++) {
min = i
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) min = j
}
if (i !== min) {
let temp = array[i]
array[i] = array[min]
array[min] = temp
}
}
return array
}
console.log(selectionSort(['a', 'c', 'z', 'g', 's', 'i']))
console.log(selectionSort([22, 1, 3, 8, 4, 2]))
// *** Algorithms: Insertion Sort ***
function insertionSort(array) {
let temp
for (let i = 1; i < array.length; i++) {
let j;
temp = array[i]
for (j = i - 1; array[j] > temp && j > -1; j--) {
array[j + 1] = array[j]
}
array[j + 1] = temp
}
return array
}
console.log(insertionSort(['a', 'c', 'z', 'g', 's', 'i']))
console.log(insertionSort([22, 1, 3, 8, 4, 2]))
// *** Algorithms: Merge function ***'
function merge(array1, array2) {
let combined = []
let i = 0
let j = 0
while(i < array1.length && j < array2.length) {
if(array1[i] < array2[j]) {
combined.push(array1[i])
i++
} else {
combined.push(array2[j])
j++
}
}
while(i < array1.length) {
combined.push(array1[i])
i++
}
while(j < array2.length) {
combined.push(array2[j])
j++
}
return combined
}
console.log(merge([1,3,7,8], [2,4,5,6]))
// *** Algorithms: Merge sort ***
function mergeSort(array) {
if(array.length === 1) return array
let mid = Math.floor(array.length/2)
let left = array.slice(0,mid)
let right = array.slice(mid)
return merge(mergeSort(left), mergeSort(right))
}
console.log(mergeSort([3,1,4,2,5,77,44,32]))
// *** Algorithms: Pivot function ***
function swap(array, firstIndex, secondIndex) {
let temp = array[firstIndex]
array[firstIndex] = array[secondIndex]
array[secondIndex] = temp
}
function pivot(array, pivotIndex=0, endIndex=array.length-1) {
let swapIndex = pivotIndex
for(let i= pivotIndex + 1; i<= endIndex; i++){
if(array[i] < array[pivotIndex]) {
swapIndex++
swap(array, swapIndex, i)
}
}
swap(array, pivotIndex, swapIndex)
return swapIndex
}
let myArray = [4,6,1,7,3,2,5]
console.log(pivot(myArray))
console.log(myArray)
// *** Algorithms: Quick sort ***
function quickSort(array, left=0, right=array.length-1) {
if(left < right) {
let pivotIndex = pivot(array, left, right)
quickSort(array, left, pivotIndex-1)
quickSort(array, pivotIndex+1, right)
}
return array
}
console.log(quickSort([4,6,1,7,3,2,5]))
// *** Algorithms: Tree Traversal ***
class Node {
constructor(value) {
this.value = value
this.left = null
this.right = null
}
}
class BST {
constructor() {
this.root = null
}
insert(value) {
const newNode = new Node(value)
if (this.root === null) {
this.root = newNode
return this
}
let temp = this.root
while(true) {
if (newNode.value === temp.value) return undefined
if (newNode.value < temp.value) {
if (temp.left === null) {
temp.left = newNode
return this
}
temp = temp.left
} else {
if (temp.right === null) {
temp.right = newNode
return this
}
temp = temp.right
}
}
}
contains(value) {
if (this.root === null) return false
let temp = this.root
while(temp) {
if (value < temp.value) {
temp = temp.left
} else if (value > temp.value) {
temp = temp.right
} else {
return true
}
}
return false
}
// Breadth First Search
BFS() {
let currentNode = this.root
let results = []
let queue = []
queue.push(currentNode)
while(queue.length) {
currentNode = queue.shift()
results.push(currentNode.value)
if(currentNode.left) queue.push(currentNode.left)
if(currentNode.right) queue.push(currentNode.right)
}
return results
}
// Depth First Search Pre Order
DFSPreOrder() {
let results = []
function traverse(currentNode) {
results.push(currentNode.value)
if(currentNode.left) traverse(currentNode.left)
if(currentNode.right) traverse(currentNode.right)
}
traverse(this.root)
return results
}
// Depth First Search Post Order
DFSPostOrder() {
let results = []
function traverse(currentNode){
if(currentNode.left) traverse(currentNode.left)
if(currentNode.right) traverse(currentNode.right)
results.push(currentNode.value)
}
traverse(this.root)
return results
}
// Depth First Search In Order
DFSInOrder() {
let results = []
function traverse(currentNode) {
if(currentNode.left) traverse(currentNode.left)
results.push(currentNode.value)
if(currentNode.right) traverse(currentNode.right)
}
traverse(this.root)
return results
}
}
let myTree = new BST()
myTree.insert(47)
myTree.insert(21)
myTree.insert(76)
myTree.insert(18)
myTree.insert(27)
myTree.insert(52)
myTree.insert(82)