codinasion-archive / codinasion-monorepo

Community Monorepo
https://codinasion.org
MIT License
52 stars 167 forks source link

Write a Python program to implement ternary search #5123

Closed harshraj8843 closed 9 months ago

harshraj8843 commented 10 months ago

Description

Write a Python program to implement ternary search

Ternary search is a divide and conquer algorithm that can be used to find an element in an array. It is similar to binary search where we divide the array into two parts but in this algorithm, we divide the given array into three parts and determine which has the key (searched element).

Pseudocode

procedure ternary_search
   A ← sorted array
   value ← value to be searched
   l ← leftmost index
   r ← rightmost index

   while l ≤ r do

      partition size = (r-l)/3
      mid1 = l + partition_size
      mid2 = r - partition_size

      if A[mid1] = value
         return mid1

      if A[mid2] = value
         return mid2

      if value < A[mid1]
         set r = mid1 - 1

      else if value > A[mid2]
         set l = mid2 + 1

      else
         set l = mid1 + 1
         set r = mid2 - 1

   end while

   return -1
end procedure

Example

list = [1,2,3,4,5]
value = 4

Output : 3
How to contribute - Comment `!assign` to assign this issue to yourself - Fork this repository - Create a new branch - Save the solution in `program/program/implement-ternary-search/implement_ternary_search.py` - Commit the changes - Create a pull request
codinasion-bot[bot] commented 10 months ago

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

Parvezkhan0 commented 10 months ago

!assign

Redzwinger commented 10 months ago

!assign

codinasion-bot[bot] commented 10 months ago

Hey @Redzwinger, this issue is already assigned to @Parvezkhan0! cc/ @codinasion/codinasion

av-techspot commented 9 months ago

!assign

codinasion-bot[bot] commented 9 months ago

Hey @av-techspot, this issue is already assigned to @Parvezkhan0! cc/ @codinasion/codinasion