Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

binary sort js

<script>
// Iterative function to implement Binary Search
let iterativeFunction = function (arr, x) {
  
    let start=0, end=arr.length-1;
         
    // Iterate while start not meets end
    while (start<=end){
 
        // Find the mid index
        let mid=Math.floor((start + end)/2);
  
        // If element is present at mid, return True
        if (arr[mid]===x) return true;
 
        // Else look in left or right half accordingly
        else if (arr[mid] < x)
             start = mid + 1;
        else
             end = mid - 1;
    }
  
    return false;
}
  
// Driver code
let arr = [1, 3, 5, 7, 8, 9];
let x = 5;
  
if (iterativeFunction(arr, x, 0, arr.length-1))
    document.write("Element found!<br>");
else document.write("Element not found!<br>");
  
x = 6;
  
if (iterativeFunction(arr, x, 0, arr.length-1))
    document.write("Element found!<br>");
else document.write("Element not found!<br>");
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: qr code generator with js 
Javascript :: javascript cheatsheet 
Javascript :: class in javascript 
Javascript :: export multiple function in node js 
Javascript :: flightphp 
Javascript :: get all database react native 
Javascript :: angular lazy loading images 
Javascript :: how to use fetch api 
Javascript :: updatig state in react 
Javascript :: ejs layout 
Javascript :: form handling in react 
Javascript :: react native spinkit 
Javascript :: How to get random no. without math.random() function 
Javascript :: materal ui react range slider 
Javascript :: express 
Javascript :: .catch chain 
Javascript :: changement image js sur click 
Javascript :: math.floor + roandom 
Javascript :: polyfill for call 
Javascript :: initialize firebase app 
Javascript :: testing with jest 
Javascript :: call local function javascript 
Javascript :: get number right of the dot length javascript 
Javascript :: get dynamic value in jquery 
Javascript :: React timeago 
Javascript :: javascript this Inside Object Method 
Javascript :: key codes javascript 
Javascript :: jquery change label content 
Javascript :: redis pub or sub nodejs 
Javascript :: vue js override component css 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =