Two-Part search method
function Binary_search (Source_arr, target) {
var len = source_arr.length,
Start = 0,
End = Len-1,
Middle,middle_val;
while (start <= end) {
Middle = parseint ((start + end)/2);
Middle_val = Source_arr[middle];
if (Middle_val = = target) {
return middle;
}else if (Middle_val > target) {
end = Middle-1;
}else{
Start = middle + 1;
}
}
Return-1;
}
Time complexity: 2^x = n so time complexity is log2n, space complexity is n;
Hash table lookup (none)
Quick Sort
function Quick_sort (Source_arr, left, right) {
if (left < right) {
var key = Source_arr[left],
Start = left,
end = right;
while (Start < end) {
while (Start < end && Source_arr[end] > key) {
End--;
}
Source_arr[start] = Source_arr[end];
while (Start < end && Source_arr[start] < key) {
Start + +;
}
Source_arr[end] = Source_arr[start];
}
Source_arr[start] = key;
Quick_sort (Source_arr, left, start-1);
Quick_sort (Source_arr, start+1, right);
}
}
Time Complexity log2n~ n*n (n + n/2 + N/2 + N/4 + N/4 + N/4 + N/4 ...)
B-Tree, binary-tree traversal
JS implementation of binary search and fast sorting algorithm