Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » PHP Development Tools (PDT) » PHP's array population from the Year Planner(Is there an alternative way to do this that I haven't tried?)
PHP's array population from the Year Planner [message #1856594] Fri, 16 December 2022 12:32
Ashley coder is currently offline Ashley coderFriend
Messages: 11
Registered: October 2022
Junior Member
Determine if a specified element is present in a given array of distinct elements, which has been created by rotating a sorted array.
Reference link: https://www.interviewbit.com/blog/search-in-rotated-sorted-array/

Is there an alternative way to do this that I haven't tried?

Let input array arr = [4,5,6,7,8,9,1,2,3]
number of elements  = 9
mid index = (0+8)/2 = 4

[4,5,6,7,8,9,1,2,3]
         ^
 left   mid  right


[6,7,8,9,1,2,3,4,5]
         ^


function search( arr[], key, low, high)

        mid = (low + high) / 2

        // key not present
        if(low > high)
                return -1

        // key found
        if(arr[mid] == key)
                return mid

        // if left half is sorted.
        if(arr[low] <= arr[mid])

                // if key is present in left half.
                if (arr[low] <= key && arr[mid] >= key) 
                        return search(arr,key,low,mid-1)

                // if key is not present in left half..search right half.
                else                 
                        return search(arr,key,mid+1,high)
                end-if

        // if right half is sorted. 
        else    
                // if key is present in right half.
                if(arr[mid] <= key && arr[high] >= key) 
                        return search(arr,key,mid+1,high)

                // if key is not present in right half..search in left half.
                else
                        return search(arr,key,low,mid-1)
                end-if
        end-if  

end-function


Previous Topic:Update issue
Next Topic:The best lightweight IDE for PHP programming?
Goto Forum:
  


Current Time: Wed Oct 09 04:03:27 GMT 2024

Powered by FUDForum. Page generated in 0.02978 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top