Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Implement Ternary Search #5108

Open
harshraj8843 opened this issue Jan 9, 2024 · 1 comment
Open

Implement Ternary Search #5108

harshraj8843 opened this issue Jan 9, 2024 · 1 comment
Labels
auto-track Good First Issue Tracker program

Comments

@harshraj8843
Copy link
Contributor

harshraj8843 commented Jan 9, 2024

Description

Write a 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
### Tracking Issues
- [ ] #5110
- [ ] #5111
- [ ] #5112
- [ ] #5113
- [ ] #5114
- [ ] #5115
- [ ] #5116
- [ ] #5117
- [ ] #5118
- [ ] #5119
- [ ] #5120
- [ ] #5121
- [ ] #5122
- [ ] #5123
- [ ] #5124
- [ ] #5125
- [ ] #5126
- [ ] #5127
- [ ] #5128
- [ ] #5129
@harshraj8843 harshraj8843 added the auto-track Good First Issue Tracker label Jan 9, 2024
@codinasion-bot
Copy link

codinasion-bot bot commented Jan 9, 2024

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

@codinasion-bot codinasion-bot bot added the triage Waiting for review label Jan 9, 2024
@harshraj8843 harshraj8843 added program and removed triage Waiting for review labels Jan 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-track Good First Issue Tracker program
Projects
None yet
Development

No branches or pull requests

1 participant