JavaScript - Ternary search algorithm
JavaScript Searching Algorithm: Exercise-3 with Solution
Write a JavaScript program to find the first index of a given element in an array using the ternary search algorithm.
A ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be in the last third of the domain, then repeats on the remaining two thirds. A ternary search is an example of a divide and conquer algorithm.
Test Data:
([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 0, 5)) -> 2
([1, 2, 3, 4, 5, 6, 7, 8, 9], 9, 0, 8)) -> 8
([1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 0, 8)) -> -1
([1, 2, 3, 4, 5, 6, 7, 8, 9], 4, 6, 8)) -> -1
Sample Solution:
JavaScript Code:
const ternary_Search = (arr, target, left, right) => {
if (right >= left) {
let mid1 = Math.floor(left + (right - left) /3);
let mid2 = Math.floor(right - (right - left) /3);
if (arr[mid1] == target) {
return mid1;
}
else if (arr[mid2] == target) {
return mid2;
}
if (target < arr[mid1]) {
return ternary_Search(arr, target, left, mid1 - 1);
}
else if (target > arr[mid2]) {
return ternary_Search(arr, target, mid2 + 1, right);
}
else {
return ternary_Search(arr, target, mid1 + 1, mid2 - 1);
}
}
return -1;
}
console.log(ternary_Search([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 0, 5))
console.log(ternary_Search([1, 2, 3, 4, 5, 6, 7, 8, 9], 9, 0, 8))
console.log(ternary_Search([1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 0, 8))
console.log(ternary_Search([1, 2, 3, 4, 5, 6, 7, 8, 9], 4, 6, 8))
Sample Output:
2 8 -1 -1
Flowchart:

Live Demo:
See the Pen javascript-searching-algorithm-exercise-3 by w3resource (@w3resource) on CodePen.
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Improve this sample solution and post your code through Disqus
Previous: Linear search algorithm.
Next: Jump search algorithm.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
defineProperty method
const person = { name: 'Owen' }; Object.defineProperty(person, 'age', { value: 21 }); console.log(person); console.log(Object.keys(person));
With the defineProperty method, we can add new properties to an object, or modify existing ones. When we add a property to an object using the defineProperty method, they are by default not enumerable. The Object.keys method returns all enumerable property names from an object, in this case only "name".
Properties added using the defineProperty method are immutable by default. You can override this behavior using the writable, configurable and enumerable properties. This way, the defineProperty method gives you a lot more control over the properties you're adding to an object.
Ref: https://bit.ly/323Y0P6
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook