function sequenceSearch(arr, value) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
return i
}
}
return -1
}
function binarySearch(arr, value, head, tail) {
var len = arr.length
head = typeof head != 'number' ? 0 : head
tail = typeof tail != 'number' ? len - 1 : tail
if (tail < head) {
return -1
}
var middleIndex = Math.floor((tail + head) / 2)
if (arr[middleIndex] === value) {
return middleIndex
}
if (arr[middleIndex] < value) {
return binarySearch(arr, value, middleIndex + 1, tail)
} else {
return binarySearch(arr, value, head, middleIndex - 1)
}
}
function binarySearch1(arr, value) {
var mid = Math.floor(arr.length / 2)
var low = 0
var high = arr.length
while (low <= high) {
console.log(mid)
if (arr[mid] === value)
return mid
else if (arr[mid] < value) {
low = mid + 1
} else {
high = mid - 1
}
mid = Math.floor((low + high) / 2)
}
}
function insertionSearch(arr, value, low, high) {
var len = arr.length
if (arr.length < 2) {
if (arr[0] === value)
return 0
else
return -1
}
low = typeof low != 'number' ? 0 : low
high = typeof high != 'number' ? len - 1 : high
var midIndex = low + Math.floor((value - arr[low]) / (arr[high] - arr[low]) * (high - low))
if (arr[midIndex] === value) {
return midIndex
}
else if (arr[midIndex] > value) {
return insertionSearch(arr, value, low, midIndex - 1)
}
else if (arr[midIndex] < value) {
return insertionSearch(arr, value, midIndex + 1, high)
}
}
function fibonacci(n) {
if (n < 3)
return 1
else
return fibonacci(n - 1) + fibonacci(n - 2)
}
function fibonacciSearch(arr, value) {
var len = arr.length
var low = 0
var high = len - 1
var k = 1
while (high > fibonacci(k) - 1) {
k++
}
for (let i = high + 1; i < fibonacci(k) - 1; i++) {
arr.push(arr[high])
}
console.log(arr)
while (low <= high) {
var mid = low + fibonacci(k - 1) - 1
console.log(k + " " + low + " " + high + " " + mid)
if (arr[mid] === value) {
return mid
}
else if (value < arr[mid]) {
high = mid - 1
k -= 1
}
else {
low = mid + 1
k -= 2
}
}
return -1
}
function Node(data, left, right) {
this.data = data;
this.left = left;
this.right = right;
}
function TREE() {
this.root = null
this.insertNode = insertNode
}
function insertNode(data) {
let node = new Node(data, null, null)
if (!this.root) {
this.root = node
} else {
let current = this.root
while (current) {
if (current.data > node.data) {
if (current.left)
current = current.left
else {
current.left = node
break
}
}
else {
if (current.right)
current = current.right
else {
current.right = node
break
}
}
}
}
}
var tree = new TREE
for (let i = 0; i < arr.length; i++) {
tree.insertNode(arr[i])
}
function treeSearch(tree, value) {
if (tree.root) {
current = tree.root
while (current) {
if (value > current.data) {
current = current.right
} else if (value < current.data) {
current = current.left
} else if (value === current.data) {
return 1
}
}
}
return -1
}
console.log(treeSearch(tree,20))