Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Veronica

Coder
We can use binary search to find the target element in an array. First, we need to sort the array in ascending order. Then, we can use binary search to find the target element in the sorted array. The time complexity of this approach is O(nlogn), where n is the number of elements in the Array. Came across from this Resource.

Can anyone help me with more resources?

Thanks in advance. 😊
 
Yes most case

try this code above, if you dont want programing into HTML file you can use the JSFiddle OR Codepen its online IDE for development and tests

JavaScript:
let recursiveFunction = function (arr, x, start, end) {
     
    // Base Condition
    if (start > end) return false;
 
    // Find the middle index
    let mid=Math.floor((start + end)/2);
 
    // Compare mid with given key x
    if (arr[mid]===x) return true;
       
    // If element at mid is greater than x,
    // search in the left half of mid
    if(arr[mid] > x)
        return recursiveFunction(arr, x, start, mid-1);
    else
 
        // If element at mid is smaller than x,
        // search in the right half of mid
        return recursiveFunction(arr, x, mid+1, end);
}
 
// Driver code
let arr = [1, 3, 5, 7, 8, 9];
let x = 5;
 
if (recursiveFunction(arr, x, 0, arr.length-1))
    window.alert("Element found!<br>");
else window.alert("Element not found!<br>");
 
x = 6;
 
if (recursiveFunction(arr, x, 0, arr.length-1))
   window.alert("Element found!<br>");
else window.alert("Element not found!<br>");
</script>
 
Here binary

JavaScript:
var a = [
    1,
    2,
    4,
    6,
    1,
    100,
    0,
    10000,
    3
];

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

window.alert('a,', a);

function binarySearch(arr, i) {
    var mid = Math.floor(arr.length / 2);
   window.alert(arr[mid], i);
    
    if (arr[mid] === i) {
        window.alert('match', arr[mid], i);
        return arr[mid];
    } else if (arr[mid] < i && arr.length > 1) {
        window.alert('mid lower', arr[mid], i);
        return binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
    } else if (arr[mid] > i && arr.length > 1) {
        window.alert('mid higher', arr[mid], i);
        return binarySearch(arr.splice(0, mid), i);
    } else {
        window.alert('not here', i);
        return -1;
    }
    
}
var result = binarySearch(a, 100);

window.alert(result);
 

New Threads

Buy us a coffee!

Back
Top Bottom