-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.js
More file actions
100 lines (81 loc) · 1.95 KB
/
binarySearch.js
File metadata and controls
100 lines (81 loc) · 1.95 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
//Binary search function
function binarySearch(array, x) {
var result = _quickSort(array);
var start = 0;
var end = result.length - 1;
var mid;
var midValue;
while (start <= end) {
mid = Math.ceil((start + end) / 2);
midValue = result[mid];
if (midValue == x) {
return mid;
} else if (x < midValue) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
//Sort function
function _quickSort(arr) {
var result = arr.slice();
_sortSection(result, 0, result.length);
return result;
}
function _sortSection(arr, start, length) {
if (length < 2) {
return;
}
var pivotIndex = start + length - 1;
var pivotValue = arr[pivotIndex];
for (var i = start; i < pivotIndex; i++) {
while (arr[i] > pivotValue) {
if (pivotIndex == i + 1)
{
_swap(arr, i, pivotIndex);
pivotIndex--;
continue;
}
_swap(arr, i, pivotIndex - 1);
_swap(arr, pivotIndex - 1, pivotIndex);
pivotIndex--;
}
}
_splitSection(arr, start, length, pivotIndex);
}
function _splitSection(arr, start, length, pivotIndex) {
_sortSection(arr, start, pivotIndex - start);
_sortSection(arr, pivotIndex + 1, arr.length - pivotIndex - 1);
}
//Swap function
function _swap(arr, indexOne, indexTwo) {
var temp = arr[indexOne];
arr[indexOne] = arr[indexTwo];
arr[indexTwo] = temp;
}
//Test Cases:
var testCase = [
[],
[1],
[5],
[1, 2],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[3, 7, 5, 4, 2, 1, 9, 5, 0]
];
var index = [];
var target = 5;
for (var i = 0; i < testCase.length; i++) {
index = binarySearch(testCase[i], target);
if (index == -1) {
console.log("==================================");
console.log("Original array: " + testCase[i].join(", "));
console.log(target + " not found in array");
} else {
console.log("==================================");
console.log("Original array: " + testCase[i].join(", "));
console.log(" Sorted array: " + _quickSort(testCase[i]).join(", "));
console.log(target + " was found at index " + index);
}
}