forked from Apress/js-data-structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter10(Search).js
195 lines (165 loc) · 4.61 KB
/
Chapter10(Search).js
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
function linearSearch(array, n) {
for (var i = 0; i < array.length; i++) {
if (array[i] == n) {
return true;
}
}
return false;
}
console.log(linearSearch([1,2,3,4], 4));
console.log(linearSearch([1,2,3,4], 5));
function binarySearch(array, n) {
var lowIndex = 0,
highIndex = array.length - 1;
while (lowIndex <= highIndex) {
var midIndex = Math.floor((highIndex + lowIndex) / 2);
if (array[midIndex] == n) {
return midIndex;
} else if (n > array[midIndex]) {
lowIndex = midIndex+1;
} else {
highIndex = midIndex-1;
}
}
return -1;
}
console.log(binarySearch([1,2,3,4], 4));
console.log(binarySearch([1,2,3,4], 5));
function binarySearch(array, startIndex, endIndex, value) {
if (startIndex > endIndex) {
return false;
}
var middleIndex = Math.floor((startIndex + endIndex) / 2);
if (array[middleIndex] == value) {
return middleIndex;
} else if (array[middleIndex] > value) {
return binarySearch(array, startIndex, middleIndex - 1);
} else {
return binarySearch(array, middleIndex + 1, endIndex);
}
}
binarySearch([-121, 2, 3, 4, 5, 71, 102], 0, 6, 4);
function sqrtIntNaive(number){
if(number == 0 || number == 1)
return number;
var index = 1, square = 1;
while(square < number){
if (square == number){
return square;
}
index++;
square = index*index;
}
return index;
}
sqrtIntNaive(9);
function sqrtInt(number) {
if (number == 0 || number == 1) return number;
var start = 1,
end = number,
ans;
while (start <= end) {
let mid = parseInt((start + end) / 2);
if (mid * mid == number)
return mid;
if (mid * mid < number) {
start = mid + 1; // use the upper section
ans = mid;
} else {
end = mid - 1; // use the lower section
}
}
return ans;
}
sqrtInt(9);
function sqrtDouble(number) {
var threshold = 0.1;
//9 try middle,
var upper = number;
var lower = 0;
var middle;
while (upper - lower > threshold) {
middle = (upper + lower) / 2;
if (middle * middle > number) {
upper = middle;
} else {
lower = middle;
}
}
return middle
}
sqrtDouble(9); // 3.0234375
function findTwoSum(array, sum) {
for(let i=0, arrayLength = array.length; i<arrayLength;i++){
for(let j=i;j<arrayLength;j++){
if(array[j]+array[i]){
return true;
}
}
}
return false;
}
function findTwoSum(array, sum){
var store = {};
for(let i=0, arrayLength = array.length; i<arrayLength;i++){
console.log(store);
if(store[sum-array[i]]){
return true;
}else{
store[sum-array[i]] = array[i];
}
}
return false;
}
console.log(findTwoSum([1,2,3],5));
function findOnlyOnce(arr, low, high) {
if (low > high) {
return null;
}
if (low == high) {
return arr[low];
}
var mid = Math.floor((high+low)/2);
if (mid%2 == 0) {
if (arr[mid] == arr[mid+1]) {
return findOnlyOnce(arr, mid+2, high);
} else {
return findOnlyOnce(arr, low, mid);
}
} else {
if (arr[mid] == arr[mid-1]) {
return findOnlyOnce(arr, mid+1, high);
} else {
return findOnlyOnce(arr, low, mid-1);
}
}
}
function findOnlyOnceHelper(arr) {
return findOnlyOnce(arr, 0, arr.length);
}
findOnlyOnceHelper([ 1, 1, 2, 4, 4, 5, 5, 6, 6 ]);
### 4. Find the minimum element in a sorted and rotated array
function findMinRotated(arr, low, high){
// This condition is needed to handle the case when array is not
// rotated at all
if (high < low) return arr[0];
// If there is only one element left
if (high == low) return arr[low];
// Find mid
var mid = Math.floor((low+high)/2);
// Check if element (mid+1) is minimum element. Consider
// the cases like {3, 4, 5, 1, 2}
if (mid < high && arr[mid+1] < arr[mid])
return arr[mid+1];
// Check if mid itself is minimum element
if (mid > low && arr[mid] < arr[mid - 1])
return arr[mid];
// Decide whether we need to go to left half or right half
if (arr[high] > arr[mid])
return findMinRotated(arr, low, mid-1);
return findMinRotated(arr, mid+1, high);
}
function findMinRotatedHelper(arr) {
return findMinRotated(arr, 0, arr.length-1);
}
findMinRotatedHelper([5,6,1,2,3]);