Buscar un elemento en una array ordenada y rotada

Dada una array ordenada y rotada arr[] de tamaño N y una clave , la tarea es encontrar la clave en la array.

Nota: Encuentre el elemento en tiempo O(logN) y suponga que todos los elementos son distintos.

Ejemplo:  

Entrada: arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}, clave = 3 Salida
: Encontrado en el índice 8

Entrada: arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}, clave = 30 Salida
: No encontrado

Entrada: arr[] = {30, 40, 50, 10, 20}, clave = 10   
Salida: Encontrado en el índice 3

Enfoque 1 (Encontrar el pivote donde ha ocurrido la rotación): La idea principal para resolver el problema es la siguiente. 

La idea es encontrar el punto de pivote, dividir la array en dos sub-arrays y realizar una búsqueda binaria.

La idea principal para encontrar un pivote es: 

  • Para una array ordenada (en orden creciente) y rotada, el elemento pivote es el único elemento para el cual el siguiente elemento es más pequeño que él.
  • Usando la búsqueda binaria basada en la idea anterior, se puede encontrar el pivote.
    • Se puede observar que para un espacio de búsqueda de índices en el rango [l, r] donde el índice medio es mid
      • Si la rotación ha ocurrido en la mitad izquierda, entonces obviamente el elemento en l será mayor que el que está en el medio .
      • De lo contrario, se ordenará la mitad izquierda, pero el elemento de la mitad será mayor que el de r .
  • Después de encontrar el pivote, divida la array en dos sub-arrays.
  • Ahora los subconjuntos individuales están ordenados para que el elemento pueda buscarse mediante la búsqueda binaria.

Siga los pasos mencionados a continuación para implementar la idea:  

  • Descubra el punto de pivote mediante la búsqueda binaria. Estableceremos el puntero bajo como el primer índice de array y alto con el último índice de array.
    • A partir del alto y el bajo calcularemos el valor medio. 
    • Si el valor de mid-1 es mayor que el de mid , devuelva ese valor como pivote.
    • De lo contrario, si el valor en mid+1 es menor que mid , devuelve el valor medio como pivote.
    • De lo contrario, si el valor en la posición baja es mayor que en la posición media , considere la mitad izquierda. De lo contrario, considere la mitad derecha.
  • Divida la array en dos sub-arrays según el pivote que se encontró.
  • Ahora llame a la búsqueda binaria para uno de los dos sub-arreglos.
    • Si el elemento es mayor que el elemento 0 , busque en la array izquierda
    • De lo contrario, busque en la array correcta.
  • Si el elemento se encuentra en el subconjunto seleccionado, devuelva el índice
  • Elsereturn -1 .

Siga la siguiente ilustración para una mejor comprensión

Ilustración:

Considere arr[] = {3, 4, 5, 1, 2}, clave = 1

Hallazgo de pivote:

bajo = 0, alto = 4:
        => medio = 2
        => arr[medio] = 5, arr[medio + 1] = 1
        => arr[medio] > arr[medio +1],
        => Por lo tanto, el pivote = medio = 2

La array se divide en dos partes {3, 4, 5}, {1, 2}
Ahora, de acuerdo con las condiciones y la clave, necesitamos encontrar en la parte {1, 2} 

Hallazgo clave:

Aplicaremos la búsqueda binaria en {1, 2}. 
low = 3 , high = 4.
             => mid = 3
            => arr[mid] = 1 , key = 1, por lo tanto arr[mid] = coincidencias clave.
            => El índice requerido = mid = 3

Entonces el elemento se encuentra en el índice 3 .

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ Program to search an element
// in a sorted and pivoted array
 
#include <bits/stdc++.h>
using namespace std;
 
// Standard Binary Search function
int binarySearch(int arr[], int low, int high, int key)
{
    if (high < low)
        return -1;
 
    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);
}
 
// Function to get pivot. For array 3, 4, 5, 6, 1, 2
// it returns 3 (index of 6)
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);
}
 
// Searches an element key in a pivoted
// sorted array arr[] of size n
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);
}
 
// Driver program to check above functions
int main()
{
    // Let us search 3 in below array
    int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int key = 3;
 
    // Function calling
    cout << "Index of the element is : "
         << pivotedBinarySearch(arr1, n, key);
 
    return 0;
}

C

/* Program to search an element in
   a sorted and pivoted array*/
#include <stdio.h>
 
int findPivot(int[], int, int);
int binarySearch(int[], int, int, int);
 
/* Searches an element key in a pivoted
   sorted array arrp[] of size n */
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) */
int findPivot(int arr[], int low, int high)
{
    // base cases
    if (high < low)
        return -1;
    if (high == low)
        return low;
 
    int mid = (low + high) / 2; /*low + (high - low)/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*/
int binarySearch(int arr[], int low, int high, int key)
{
    if (high < low)
        return -1;
    int mid = (low + high) / 2; /*low + (high - low)/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);
}
 
/* Driver program to check above functions */
int main()
{
    // Let us search 3 in below array
    int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int key = 3;
    printf("Index of the element is : %d",
           pivotedBinarySearch(arr1, n, key));
    return 0;
}

Java

/* 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));
    }
}

Python3

# Python Program to search an element
# in a sorted and pivoted array
 
# Searches an element key in a pivoted
# sorted array arrp[] of size n
def pivotedBinarySearch(arr, n, key):
 
    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)
def findPivot(arr, low, high):
 
    # base cases
    if high < low:
        return -1
    if high == low:
        return low
 
    # low + (high - low)/2;
    mid = int((low + high)/2)
 
    if mid < high and arr[mid] > arr[mid + 1]:
        return mid
    if mid > low and 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
def binarySearch(arr, low, high, key):
 
    if high < low:
        return -1
 
    # low + (high - low)/2;
    mid = int((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)
 
 
# Driver program to check above functions
# Let us search 3 in below array
if __name__ == '__main__':
    arr1 = [5, 6, 7, 8, 9, 10, 1, 2, 3]
    n = len(arr1)
    key = 3
    print("Index of the element is : ", \
          pivotedBinarySearch(arr1, n, key))
 
# This is contributed by Smitha Dinesh Semwal

C#

// C# program to search an element
// in a sorted and pivoted array
using System;
 
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);
    }
 
    // Driver Code
    public static void Main()
    {
        // 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;
        Console.Write("Index of the element is : "
                      + pivotedBinarySearch(arr1, n, key));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP Program to search an element
// in a sorted and pivoted array
 
// Standard Binary Search function
function binarySearch($arr, $low,
                      $high, $key)
{
    if ($high < $low)
        return -1;
         
    /*low + (high - low)/2;*/   
    $mid = floor($low + $high) / 2;
     
    if ($key == $arr[$mid])
        return $mid;
         
    if ($key > $arr[$mid])
        return binarySearch($arr, ($mid + 1),
                                $high, $key);
         
    else
        return binarySearch($arr, $low,
                      ($mid -1), $key);
}
 
// Function to get pivot.
// For array 3, 4, 5, 6, 1, 2
// it returns 3 (index of 6)
function findPivot($arr, $low, $high)
{
     
    // base cases
    if ($high < $low)
        return -1;
    if ($high == $low)
        return $low;
     
    /*low + (high - low)/2;*/
    $mid = ($low + $high)/2;
    if ($mid < $high and $arr[$mid] >
                     $arr[$mid + 1])
        return $mid;
         
    if ($mid > $low and $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);
}
 
// Searches an element key
// in a pivoted sorted array
// arr[] of size n */
function pivotedBinarySearch($arr, $n, $key)
{
    $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);
}
 
// Driver Code
// Let us search 3
// in below array
$arr1 = array(5, 6, 7, 8, 9, 10, 1, 2, 3);
$n = count($arr1);
$key = 3;
 
// Function calling
echo "Index of the element is : ",
      pivotedBinarySearch($arr1, $n, $key);
             
// This code is contributed by anuj_67.
?>

Javascript

<script>
/* JavaScript Program to search an element
   in a sorted and pivoted array*/
 
/* Standard Binary Search function*/
function binarySearch( arr, low,
                  high, key){
    if (high < low)
        return -1;
 
    let mid = Math.floor((low + high) / 2); /*low + (high - low)/2;*/
    if (key == arr[mid])
        return mid;
 
    if (key > arr[mid])
        return binarySearch(arr, (mid + 1), high, key);
 
    // else
    return binarySearch(arr, low, (mid - 1), key);
}
 
/* Function to get pivot. For array 3, 4, 5, 6, 1, 2
   it returns 3 (index of 6) */
function findPivot( arr, low, high){
    // base cases
    if (high < low)
        return -1;
    if (high == low)
        return low;
 
    let mid = Math.floor((low + high) / 2); /*low + (high - low)/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);
}
 
/* Searches an element key in a pivoted
   sorted array arr[] of size n */
function pivotedBinarySearch( arr, n, key){
    let 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);
}
 
/* Driver program to check above functions */
// Let us search 3 in below array
let arr1 = [ 5, 6, 7, 8, 9, 10, 1, 2, 3 ];
let n = arr1.length;
let key = 3;
// Function calling
document.write( "Index of the element is : "
         + pivotedBinarySearch(arr1, n, key));
 
</script>
Producción

Index of the element is : 8

Complejidad de tiempo: O (log N) La búsqueda binaria requiere comparaciones de log n para encontrar el elemento.
Complejidad espacial: O(1)

Gracias a Ajay Mishra por proporcionar la solución anterior.

Enfoque 2 (Búsqueda binaria directa en Array sin encontrar Pivot): 

La idea es que, en lugar de dos o más pasadas de búsqueda binaria, el resultado se pueda encontrar en una pasada de búsqueda binaria. 

La idea es crear una función recursiva para implementar la búsqueda binaria donde la región de búsqueda es [l, r]. Para cada llamada recursiva:

  • Calculamos el valor medio como mid = (l + h) / 2
  • Luego intente averiguar si l a mid está ordenado, o (mid+1) a h está ordenado
  • En base a eso, decida la siguiente región de búsqueda y continúe haciéndolo hasta que se encuentre el elemento o l supere h .

Siga los pasos mencionados a continuación para implementar la idea:  

  • Use una función recursiva para implementar la búsqueda binaria para encontrar la clave:
    • Encuentra el punto medio mid = (l + h)/2 
    • Si la clave está presente en el punto medio, devuelva mid .
    • De lo contrario, si el valor en l es menor que el de mid, entonces arr[l . . . mid] está ordenado
      • Si la clave a buscar se encuentra en el rango de arr[l] a arr[mid] , recurra para arr[l . . . medio].
      • De lo contrario, se repite para arr[mid+1 . . . h]  
    • De lo contrario arr[mid+1. . . h] está ordenado: 
      • Si la clave a buscar se encuentra en el rango de arr[mid+1] a arr[h] , recurra para arr[mid+1. . . h].
      • De lo contrario recurra para arr[l. . . medio] 

Siga la siguiente ilustración para una mejor comprensión:

Ilustración:

Entrada arr[] = {3, 4, 5, 1, 2} , clave = 1
Inicialmente bajo = 0, alto = 4.

bajo = 0, alto = 4:
        => medio = 2
        => arr[medio] = 5, que no es el valor deseado.
        => arr[bajo] < arr[medio] Entonces, la mitad izquierda está ordenada.
        => clave < arr[bajo], por lo que la siguiente región de búsqueda es 3 a 4.

low = 3, high = 4:
        => mid = 3
        => arr[mid] = 1 = key
        => Entonces el elemento se encuentra en el índice 3.

El elemento se encuentra en el índice 3 .

A continuación se muestra la implementación de la idea anterior:  

C++

// Search an element in sorted and rotated
// array using single pass of Binary Search
#include <bits/stdc++.h>
using namespace std;
 
// Returns index of key in arr[l..h] if
// key is present, otherwise returns -1
int search(int arr[], int l, int h, int key)
{
    if (l > h)
        return -1;
 
    int mid = (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
int main()
{
    int arr[] = { 4, 5, 6, 7, 8, 9, 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 3;
    int i = search(arr, 0, n - 1, key);
 
    if (i != -1)
        cout << "Index: " << i << endl;
    else
        cout << "Key not found";
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

// Search an element in sorted and rotated
// array using single pass of Binary Search
#include <stdio.h>
 
// Returns index of key in arr[l..h] if
// key is present, otherwise returns -1
int search(int arr[], int l, int h, int key)
{
    if (l > h)
        return -1;
 
    int mid = (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
int main()
{
    int arr[] = { 4, 5, 6, 7, 8, 9, 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 3;
    int i = search(arr, 0, n - 1, key);
 
    if (i != -1)
        printf("Index: %d\n", i);
    else
        printf("Key not found");
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java

/* Java program to search an element in
   sorted and rotated array using
   single pass of Binary Search*/
 
class Main {
    // Returns index of key in arr[l..h]
    // if key is present, otherwise returns -1
    static int search(int arr[], int l, int h, int key)
    {
        if (l > h)
            return -1;
 
        int mid = (l + h) / 2;
        if (arr[mid] == key)
            return mid;
 
        /* If arr[l...mid] first subarray 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);
    }
 
    // main function
    public static void main(String args[])
    {
        int arr[] = { 4, 5, 6, 7, 8, 9, 1, 2, 3 };
        int n = arr.length;
        int key = 3;
        int i = search(arr, 0, n - 1, key);
        if (i != -1)
            System.out.println("Index: " + i);
        else
            System.out.println("Key not found");
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3

# 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
def search(arr, l, h, key):
    if l > h:
        return -1
 
    mid = (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] and key <= arr[mid]:
            return search(arr, l, mid-1, key)
        return search(arr, mid + 1, h, key)
 
    # If arr[l..mid] is not sorted, then arr[mid... r]
    # must be sorted
    if key >= arr[mid] and key <= arr[h]:
        return search(arr, mid + 1, h, key)
    return search(arr, l, mid-1, key)
 
 
# Driver program
if __name__ == '__main__':
    arr = [4, 5, 6, 7, 8, 9, 1, 2, 3]
    key = 3
    i = search(arr, 0, len(arr)-1, key)
    if i != -1:
        print("Index: % d" % i)
    else:
        print("Key not found")
 
# This code is contributed by Shreyanshi Arun

C#

/* C# program to search an element in
sorted and rotated array using
single pass of Binary Search*/
using System;
 
class GFG {
 
    // Returns index of key in arr[l..h]
    // if key is present, otherwise
    // returns -1
    static int search(int[] arr, int l, int h, int key)
    {
        if (l > h)
            return -1;
 
        int mid = (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);
 
            return search(arr, mid + 1, h, key);
        }
 
        /* If arr[l..mid] is not sorted,
        then arr[mid... r] must be sorted*/
        if (key >= arr[mid] && key <= arr[h])
            return search(arr, mid + 1, h, key);
 
        return search(arr, l, mid - 1, key);
    }
 
    // main function
    public static void Main()
    {
        int[] arr = { 4, 5, 6, 7, 8, 9, 1, 2, 3 };
        int n = arr.Length;
        int key = 3;
        int i = search(arr, 0, n - 1, key);
 
        if (i != -1)
            Console.WriteLine("Index: " + i);
        else
            Console.WriteLine("Key not found");
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// 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;
 
    $mid = 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] and
            $key <= $arr[$mid])
                return search($arr, $l,
                       $mid - 1, $key);
 
        return search($arr, $mid + 1,
                           $h, $key);
    }
 
    /* If arr[l..mid] is not
       sorted, then arr[mid... r]
       must be sorted*/
    if ($key >= $arr[$mid] and
          $key <= $arr[$h])
        return search($arr, $mid + 1,
                            $h, $key);
 
    return search($arr, $l,
             $mid-1, $key);
}
 
    // Driver Code
    $arr = array( 5, 6, 7, 8, 9, 10, 1, 2, 3 );
    $n = sizeof($arr);
    $key = 3;
    $i = search($arr, 0, $n-1, $key);
 
    if ($i != -1)
        echo "Index: ", $i, " \n";
    else
        echo "Key not found";
 
// This code is contributed by ajit
?>

Javascript

<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 +"\n");
else
    document.write("Key not found");
     
</script>
Producción

Index: 8

Complejidad temporal: O(log N). La búsqueda binaria requiere comparaciones log n para encontrar el elemento. Entonces la complejidad del tiempo es O (log n).
Complejidad espacial: O(1). Como no se requiere espacio adicional.

Gracias a Gaurav Ahirwar por sugerir la solución anterior. 

¿Cómo manejar los duplicados?  
A primera vista, no parece posible buscar en el tiempo O (Log N) en todos los casos en los que se permiten duplicados. 
Por ejemplo, considere buscar 0 en {2, 2, 2, 2, 2, 2, 2, 2, 0, 2} y {2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2 , 2}. 
Consulte el siguiente artículo para encontrar una solución a este problema: https://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-rotated-array-with-duplicates/

Artículos similares: 

Escriba comentarios si encuentra algún error en los códigos/algoritmos anteriores, o encuentre otras formas de resolver el mismo problema.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *