Programa C# para buscar un elemento en una array ordenada y rotada

Un elemento en una array ordenada se puede encontrar en el tiempo O (log n) a través de una búsqueda binaria . Pero supongamos que rotamos una array ordenada de orden ascendente en algún pivote desconocido para usted de antemano. Entonces, por ejemplo, 1 2 3 4 5 podría convertirse en 3 4 5 1 2. Inventa una forma de encontrar un elemento en el arreglo rotado en tiempo O(log n).
 

sortedPivotedArray

Ejemplo: 
 

Input  : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3};
         key = 3
Output : Found at index 8

Input  : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3};
         key = 30
Output : Not found

Input : arr[] = {30, 40, 50, 10, 20}
        key = 10   
Output : Found at index 3
 

Todas las soluciones proporcionadas aquí asumen que todos los elementos de la array son distintos.
Solución básica: 
Enfoque: 
 

  1. La idea es encontrar el punto de pivote, dividir la array en dos sub-arrays y realizar una búsqueda binaria.
  2. La idea principal para encontrar el pivote es: para una array ordenada (en orden creciente) y pivotada, el elemento pivote es el único elemento para el que el siguiente elemento es más pequeño que él.
  3. Usando la declaración anterior y el pivote de búsqueda binaria se puede encontrar.
  4. Después de encontrar el pivote, divida la array en dos sub-arrays.
  5. Ahora los subconjuntos individuales se ordenan para que el elemento se pueda buscar mediante la búsqueda binaria.

Implementación: 
 

Input arr[] = {3, 4, 5, 1, 2}
Element to Search = 1
  1) Find out pivot point and divide the array in two
      sub-arrays. (pivot = 2) /*Index of 5*/
  2) Now call binary search for one of the two sub-arrays.
      (a) If element is greater than 0th element then
             search in left array
      (b) Else Search in right array
          (1 will go in else as 1 < 0th element(3))
  3) If element is found in selected sub-array then return index
     Else return -1.

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

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.

Producción: 
 

Index of the element is : 8

Análisis de Complejidad: 
 

  • 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), No se requiere espacio adicional.

Gracias a Ajay Mishra por la solución inicial.
Solución mejorada: 
Enfoque: en lugar de dos o más pasadas de búsqueda binaria, el resultado se puede encontrar en una pasada de búsqueda binaria. La búsqueda binaria debe modificarse para realizar la búsqueda. La idea es crear una función recursiva que tome l y r como rango en la entrada y la clave.
 

1) Find middle point mid = (l + h)/2
2) If key is present at middle point, return mid.
3) Else If arr[l..mid] is sorted
    a) If key to be searched lies in range from arr[l]
       to arr[mid], recur for arr[l..mid].
    b) Else recur for arr[mid+1..h]
4) Else (arr[mid+1..h] must be sorted)
    a) If key to be searched lies in range from arr[mid+1]
       to arr[h], recur for arr[mid+1..h].
    b) Else recur for arr[l..mid] 

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

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 = 6;
        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.

Producción: 

Index: 2

Análisis de Complejidad: 
 

  • 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?  
No parece posible buscar en el tiempo O (Inicio de sesión) en todos los casos cuando 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}. 
No parece posible decidir si repetir la mitad izquierda o la mitad derecha haciendo un número constante de comparaciones en el medio.
 

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.
 

Consulte el artículo completo sobre Buscar un elemento en una array ordenada y rotada para obtener más detalles.

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 *