Questions tagged [ternary-search]

Ternary search is an efficiency that can be used to find an element in a sorted array. Use this tag for question-related to ternary search only and not the ternary operator.

Ternary search

Ternary search is an efficient searching algorithm (divide and conquer algorithm) that divides a sorted array/list into 3 parts by creating 2 midpoints then compares the element-to-be-found (say x) with the 2 midpoints.

  • if the x < midpoint1: then the array is shortened to the first of the 3 parts and the function is called again recursively.
  • elif x > midpoint2: then the last part of the array is given recursively to the function.
  • finally, if neither of the conditions is satisfied then the middle portion of the array is passed recursively.
  • This process is continued until the element is found.

Read More

15 questions
5
votes
2 answers

Is ternary search less efficient than this related algorithm?

The ternary search algorithm is a fast algorithm for finding the minimum or maximum of a unimodal function, a function that either increases and then decreases or decreases and then increases. Assume that we are dealing with a function that…
4
votes
2 answers

Can prolog answer undetermined instead of just yes or no?

Suppose I have the knowledge base likes(john,mary). person(mary). person(john). If we ask prolog whether |?- likes(mary,john) It will answer no because we did not assert that. Is there any way to make prolog answer unknown unless we explicitly…
awiebe
  • 3,257
  • 3
  • 18
  • 31
4
votes
1 answer

Solving ACM TJU 2886 - Restaurants

I am trying to solve this problem: http://acm.tju.edu.cn/toj/showp2886.html I have tried a few solutions, I will explain 2 of them. Note that both assume that cost(position) is a convex function, which means that it decreases towards the middle (in…
epicrose
  • 175
  • 7
2
votes
1 answer

Why is the average of comparisons of k-ary search is k* ln(N) / ln(k)?

I know that the function is executed ln(N)/ln(K) times;but in average does it make K operations? Questions: is there any proof that k*ln(N)/ln(K) is the average number of executions? If this formula is correct, then ternary search will be the…
2
votes
2 answers

Why is ternary search used to find the max/min of an unimodal function?

I have learned that finding the minimum/maximum of an unimodal function can be done with ternary search, an algorithm that runs in O(logN) time (where N is the size of the given search range). However, I recently had the thought that maybe we could…
Anthony Smith
  • 789
  • 1
  • 7
2
votes
1 answer

Find minimun Vertext-Cover using a ternary tree

I found some algorithms to find a minimun Vertex-Cover like using a binary search tree but I read that using a ternary tree is even better. But i can't find any info about it or think of an algorithm for it. Does somebody know how it can be done?
2
votes
1 answer

Recurrence relation for ternary search

The recurrence relation of ternary search is T(n)= T(n/3) + 4, How 4 is in recurrence relation, since in ternary search it's log to the base 3 N, so only 3 partitions should be there ?
2
votes
2 answers

Binary Search vs Ternary Search

In terms of time and space complexity, is binary search better than ternary search?
Manan Kalra
  • 41
  • 1
  • 4
1
vote
2 answers

Ternary Search to find point in in array where the difference is minimum

Let A an array of n positive integers. How can I find some index k of A such that: left = A[0] + A[1] + ... + A[k] right = A[k+1] + A[k+2] + ... + A[n] have the minimum absolute difference (that is, abs(left - right) is minimum) ? As the absolute…
Daniel
  • 5,752
  • 4
  • 21
  • 48
0
votes
0 answers

A root of a "saw-like" function

Say, we have f(t) = v * t + A * sin(w * t). I call such functions "saw-like": I want to solve saw(t) = C, that is, find a root of saw(t) - C (still "saw-like"). I tried writing down a ternary search for function abs(saw(t) - C) to find its minima.…
Zhiltsoff Igor
  • 1,696
  • 5
  • 21
0
votes
1 answer

Double usage of scanf() depends on call order

I wrote a program in C which takes as an input a value and an ordered Array of integers and performs a ternary search to find the value(if it exists) inside the Array. I have seen all the possible problems with the usage of scanf and the related…
vkoukou
  • 121
  • 1
  • 11
0
votes
1 answer

How can ternary search be applied for the SPOJ challenge - KOPC12A

I was trying to solve the KOPC12A problem in SPOJ. Link to problem: http://www.spoj.com/problems/KOPC12A/ Problem in brief: Given n buildings, each of different height(number of bricks), with each building having a cost for adding or removing a…
Balasubramanian
  • 829
  • 2
  • 9
  • 15
0
votes
2 answers

Ternary Search just like Binary search but dividing items by 3

There is a function named "test" below. My program cannot pass the test function. This is my code for ternary search. Ternary Search is like binary search but instead of dividing all of the elements by two, you divide them by three. To use ternary…
-1
votes
1 answer

Ternary search won't return answer

I have written the following code for a recursive ternary function: def ternary_search(start,stop,x,arr): pos1 = start + (stop-start)//3 pos2 = stop - (stop - start)//3 if (pos1<=pos2): val1 = arr[pos1] val2 =…
Yatin
  • 2,348
  • 6
  • 20
  • 38
-5
votes
1 answer

How to implement ternary search in C?

Possible Duplicate: Ternary search in C Write tertiary [ternary] search program. Notes: Tertiary search is similar to binary search. In binary search we consider two parts of the array and select one part as the next search space. In tertiary…
JAY G
  • 533
  • 2
  • 10
  • 21