How can i use a binary search to compare two arrays and count the number of times it matched, i did find some here where one item was compared to an array..follwing is regular one for example ..thanx for the help.
var a = ["1", "2", "3", "4", "5", "6", "7"];
var b = ["8", "1", "3", "9", "4", "6", "8"];
var count = 0;
for (i = 0; i < a.length; i++) {
for (j = 0; j < b.length; j++) {
if (a[i] == b[j]) count += 1;
}
}
document.write(count);
I tried to do this ..
for (i = 0; i < a.length; i++) {
var left = 0;
var right = b.length - 1;
while (left <= right) {
var mid = parseInt((left + right) / 2);
if (b[mid] == a[i]) {
count += 1;
} else if (b[mid] < a[i]) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
}
document.write(count);
Linearly go through the
barray and use a binary search of theaarray.