Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

search in rotated sorted array

//pseudocode
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
Comment

Search in a Rotated Array

class Solution{

    public:

    int search(int A[], int l, int h, int key){

    //complete the function here

    for(int i=0;i<h+1;i++)

    {

        if(A[i]==key)

        {

            return i;

        }

    }

    return -1;

   

    }

}.
Comment

Search an elemnt in a sorted and rotated array

<script>
  
// Search an element in sorted and rotated
// array using single pass of Binary Search
  
// Returns index of key in arr[l..h] if
// key is present, otherwise returns -1
function search(arr, l, h, key){
    if (l > h)
        return -1;
  
    let mid = Math.floor((l + h) / 2);
    if (arr[mid] == key)
        return mid;
  
    /* If arr[l...mid] is sorted */
    if (arr[l] <= arr[mid]) {
        /* As this subarray is sorted, we can quickly
        check if key lies in half or other half */
        if (key >= arr[l] && key <= arr[mid])
            return search(arr, l, mid - 1, key);
        /*If key not lies in first half subarray, 
           Divide other half  into two subarrays,
           such that we can quickly check if key lies 
           in other half */
        return search(arr, mid + 1, h, key);
    }
  
    /* If arr[l..mid] first subarray is not sorted, 
    then arr[mid... h]
    must be sorted subarray */
    if (key >= arr[mid] && key <= arr[h])
        return search(arr, mid + 1, h, key);
  
    return search(arr, l, mid - 1, key);
}
  
// Driver program
let arr = [ 4, 5, 6, 7, 8, 9, 1, 2, 3 ];
let n = arr.length;
let key = 3;
let i = search(arr, 0, n - 1, key);
if (i != -1)
    document.write("Index: " +i +"
");
else
    document.write("Key not found");
      
</script>
Comment

Search in Rotated Sorted Array

class Solution {
public:
    int search(vector<int>& nums, int target) {
        
    }
};
Comment

Search in Rotated Sorted Array

class Solution {
    public int search(int[] nums, int target) {
        
    }
}
Comment

Search in Rotated Sorted Array



int search(int* nums, int numsSize, int target){

}
Comment

Search in Rotated Sorted Array

public class Solution {
    public int Search(int[] nums, int target) {
        
    }
}
Comment

Search in Rotated Sorted Array

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
    
};
Comment

Search in Rotated Sorted Array

# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer}
def search(nums, target)
    
end
Comment

Search in Rotated Sorted Array

class Solution {
    func search(_ nums: [Int], _ target: Int) -> Int {
        
    }
}
Comment

Search in Rotated Sorted Array

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer
     */
    function search($nums, $target) {
        
    }
}
Comment

Search in Rotated Sorted Array

function search(nums: number[], target: number): number {

};
Comment

: Search an element in rotated and sorted array

/* Java program to search an element
   in a sorted and pivoted array*/
 
class Main {
 
    /* Searches an element key in a
       pivoted sorted array arrp[]
       of size n */
    static int pivotedBinarySearch(int arr[], int n,
                                   int key)
    {
        int pivot = findPivot(arr, 0, n - 1);
 
        // If we didn't find a pivot, then
        // array is not rotated at all
        if (pivot == -1)
            return binarySearch(arr, 0, n - 1, key);
 
        // If we found a pivot, then first
        // compare with pivot and then
        // search in two subarrays around pivot
        if (arr[pivot] == key)
            return pivot;
        if (arr[0] <= key)
            return binarySearch(arr, 0, pivot - 1, key);
        return binarySearch(arr, pivot + 1, n - 1, key);
    }
 
    /* Function to get pivot. For array
       3, 4, 5, 6, 1, 2 it returns
       3 (index of 6) */
    static int findPivot(int arr[], int low, int high)
    {
        // base cases
        if (high < low)
            return -1;
        if (high == low)
            return low;
 
        /* low + (high - low)/2; */
        int mid = (low + high) / 2;
        if (mid < high && arr[mid] > arr[mid + 1])
            return mid;
        if (mid > low && arr[mid] < arr[mid - 1])
            return (mid - 1);
        if (arr[low] >= arr[mid])
            return findPivot(arr, low, mid - 1);
        return findPivot(arr, mid + 1, high);
    }
 
    /* Standard Binary Search function */
    static int binarySearch(int arr[], int low, int high,
                            int key)
    {
        if (high < low)
            return -1;
 
        /* low + (high - low)/2; */
        int mid = (low + high) / 2;
        if (key == arr[mid])
            return mid;
        if (key > arr[mid])
            return binarySearch(arr, (mid + 1), high, key);
        return binarySearch(arr, low, (mid - 1), key);
    }
 
    // main function
    public static void main(String args[])
    {
        // Let us search 3 in below array
        int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 };
        int n = arr1.length;
        int key = 3;
        System.out.println(
            "Index of the element is : "
            + pivotedBinarySearch(arr1, n, key));
    }
}
Comment

search in a rotated sorted array

class Solution {
    public int search(int[] nums, int target) {
        int start = 0, end = nums.length - 1;
        while (start <= end) {
            int mid = ( start + end) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[start] <= nums[mid]) {
                if (nums[start] <= target && target < nums[mid]) {
                    end = mid -1;
                }
                else {
                    start = mid + 1;
                }
            }
            else{
                if (nums[mid] < target && target <= nums[end]) {
                    start = mid + 1;
                }
                else {
                    end = mid -1;
                }
            }
        }
        return -1;
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: use prism to render markdown in next js with gray-matter 
Javascript :: req.session undefined express node js 
Javascript :: js number power/exponetion 
Javascript :: react native avoid keyboard when multiline 
Javascript :: array inside array javascript 
Javascript :: events in node js 
Javascript :: kth smallest element in an array js 
Javascript :: difference between usecallback and usememo 
Javascript :: javascript detect video change to muted 
Javascript :: how to fetch devto api 
Javascript :: js if on cellular network 
Javascript :: Check if the same text is repeated javascript todo-app 
Javascript :: findOneAndUpdate many fields 
Javascript :: react-native-wagmi-charts 
Javascript :: object wrappers in javascript 
Javascript :: Change tilte alert 
Javascript :: js draw number in range 
Javascript :: function solution(n) { } 
Javascript :: Pointing Subdomain To A Next.js Page On Vercel 
Javascript :: javascript assigning index number to row in table 
Javascript :: Setting the default value in the drop down list in AngularJS 
Javascript :: tabbarbadge style react native 
Javascript :: Understanding higher order JavaScript functions 
Javascript :: How do I change this React Navigation v5 code to v6 
Javascript :: javascript is nodelist 
Javascript :: How to limit properties of a JSON object given array of property names using JQ 
Javascript :: react native communications 
Javascript :: Clear for me API jquery 
Javascript :: bullet mechanism in phaser 
Javascript :: for in loop of javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =