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

17 questions
-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 =…
-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
  • 553
  • 2
  • 12
  • 21
1
2