Binary search algorithm in JavaScript
How do I make an binary search algorithm in javascript?
What is a binary search algorithm in javascript? How do you make a binary search algorithm in javascript? This script and codes were developed by Tomoyuki Kashiro on 19 January 2023, Thursday.
Binary search algorithm in JavaScript - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Binary search algorithm in JavaScript</title>
</head>
<body> <h1>Binary search algorithm in JavaScript</h1>
<p>Please check javascript code.</p> <script src="js/index.js"></script>
</body>
</html>
Binary search algorithm in JavaScript - Script Codes JS Codes
/*************************************** * search ***************************************/
var binarySearch = function(list, target){ var middleIndex, middle, result = 'nothing'; while(list.length > 0){ var middleIndex = Math.floor(list.length/2); var middle = list[middleIndex]; if(middle === target){ result = target; break; } if(middle > target){ list = list.slice(0,middleIndex); }else{ list = list.slice(middleIndex+1); } } return result;
};
/*************************************** * search using closure ***************************************/
//var binarySearch = function(list, target){
// var result;
//
// (function search(list, target){
// var middleIndex = Math.floor(list.length/2);
// var middle = list[middleIndex];
//
// if(middle === target){
// result = target;
// return;
// }
//
// if(middle > target){
// list = list.slice(0,middleIndex);
// }else{
// list = list.slice(middleIndex+1);
// }
//
// if(list.length === 0){
// result = 'nothing';
// }else{
// search(list, target);
// }
//
// })(list, target);
//
// return result;
//};
/*************************************** * main ***************************************/
var list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19], target = 15, result = binarySearch(list, target);
console.log(list);
console.log('index of tartget(' + target + ') is ' + result);
Developer | Tomoyuki Kashiro |
Username | Tkashiro |
Uploaded | January 19, 2023 |
Rating | 3 |
Size | 1,798 Kb |
Views | 10,120 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Auto complete | 1,872 Kb |
Mostly fluid | 2,386 Kb |
Bubble sort algorithm in JavaScrip | 1,713 Kb |
Manage Async Animation by using jQuery.DeferredPipeline | 4,751 Kb |
Button state test | 2,954 Kb |
Linear search algorithm in JavaScript | 1,698 Kb |
CSS multi-column layout | 2,627 Kb |
Page transition | 2,767 Kb |
CSS Slideshow | 2,721 Kb |
Media Fragments URI | 1,753 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Haml Calendar | Katydecorah | 5,643 Kb |
Sencha Touch 2.3.1 Basic Grid Example | Trozdol | 2,770 Kb |
Drag in vanilla js using dotval math instead of translate | Paulq | 2,662 Kb |
TheCalendar.js | The-teacher | 6,330 Kb |
Week7 replicate | Hwcasis | 1,620 Kb |
SVG Transform vs CSS Transform | AmeliaBR | 4,175 Kb |
Apple website | Jds317 | 1,835 Kb |
Flat UI Button | Honchoman | 2,289 Kb |
A Pen by Tosh | Panev | 2,586 Kb |
Flat UI - Checkbox FIX | ARS | 2,663 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!