Question

i need to understand the right way to compair a binary search with string match in...

i need to understand the right way to compair a binary search with string match in the same input value in JavaScript
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Binary Search in Javascript:

Binary Search is glancing through procedure which manages Divide and Conquer approach. It used to glance through any part in a masterminded bunch.Binary search is much faster with Time Complexity of O(logN).

Code:

var a = [6,5,1,2,3,7,9,4,8];

a.sort(function (x, y) {
return x - y;
});

console.log('Array', a);

function binarySearch(ar, i) {
var mid = Math.floor(ar.length / 2);
console.log(ar[mid], i);

if (ar[mid] === i) {
console.log('match', ar[mid], i);
return ar[mid];
} else if (ar[mid] < i && ar.length > 1) {
console.log('mid lower', ar[mid], i);
binarySearch(ar.splice(mid, Number.MAX_VALUE), i);
} else if (ar[mid] > i && ar.length > 1) {
console.log('mid higher', ar[mid], i);
binarySearch(ar.splice(0, mid), i);
} else {
console.log('Not found', i);
return -1;
}

}
var res = binarySearch(a, 100);
console.log(res);

Add a comment
Know the answer?
Add Answer to:
i need to understand the right way to compair a binary search with string match in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Enhanced Binary Search Consider the binary search algorithm in Section 14.6. If no match is found,...

    Enhanced Binary Search Consider the binary search algorithm in Section 14.6. If no match is found, the search method returns -1. Modify the method so that if a is not found, the method returns -k - 1, where k is the position before which the element should be inserted. (This is the same behavior as Arrays.binarySearch.) Upload a file named BinarySearcher.java that has the same methods the binary search class in Section 14.6. Test Case 2 Test Case 1 Console...

  • A Binary Search Tree is a binary tree where nodes are ordered in the following way:...

    A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10,          6,           4,            8,            18,          15,          21 Please type...

  • How do i make a recursive binary search that compares two String arrays in java?

    How do i make a recursive binary search that compares two String arrays in java?

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

  • I need to build an optimal binary search tree with an array of strings and frequency...

    I need to build an optimal binary search tree with an array of strings and frequency received from the user.in C

  • Write a javascript function to match string starting with .AB or AB The string should either...

    Write a javascript function to match string starting with .AB or AB The string should either start with .AB or AB followed by any characters. i am getting confused how to form regular expression and tech and match using function . the string should match with the first two characters AB or first three characters .AB then only alert statement("string is correct") otherwise alert statement ("String is not correct") input1 : .AB2e234324 output: string is correct input2 : AB44r4r44r output:...

  • I need help on question 12 please An inorder tree traversal of a binary search tree...

    I need help on question 12 please An inorder tree traversal of a binary search tree produces a listing of the tree nodes in alphabetical or numerical order. Construct a binary search tree for "To be or not to be, that is the question, " and then do an inorder traversal. Construct a binary search tree for "In the high and far off times the Elephant, O Best Beloved, had no trunk, " and then do an inorder traversal.

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • please I need it urgent thanks algorithms 2 Binary Search Trees- 10 points (5 points each)...

    please I need it urgent thanks algorithms 2 Binary Search Trees- 10 points (5 points each) 1. Write pseudocode for an algorithm that takes in the root of a binary tree and returns whether or not the tree is a legal binary search tree. 2. Write pseudocode for an algorithm that takes in the root of a binary search tree and prints the keys in sorted order.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT