Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
¿Cómo puede optimizar la búsqueda lineal si se aplica en una lista ordenada de elementos?

En una lista desordenada de elementos, debe verificar cada elemento hasta que encuentre una coincidencia. ¿Cómo puede optimizar la búsqueda lineal si se aplica en una lista ordenada de elementos?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Una búsqueda lineal tiene una complejidad de tiempo O(n). Si se sabe que la lista está ordenada, y suponiendo que admita un acceso aleatorio O(1) (por ejemplo, se implementa como una matriz en la memoria continua), podría usar la búsqueda binaria con una complejidad de tiempo de O(log(n)) .

over 4 years ago · Santiago Trujillo Report

0

Si la lista está ordenada, entonces podría usar la búsqueda binaria. En el peor de los casos O(log n) y en el mejor de los casos O(1).

Ejemplo de implementación iterativa:

 public int binSearch(int[] sortedArr, int k, int l, int h) { int i = Integer.MAX_VALUE; while (l <= h) { int mid = (l + h) / 2; if (sortedArr[mid] < k) { low = mid + 1; } else if (sortedArr[mid] > k) { high = mid - 1; } else if (sortedArr[mid] == k) { i = mid; break; } } return i; }
over 4 years ago · Santiago Trujillo Report

0

Si tiene una distribución uniforme de números aleatorios, puede hacer todo lo posible y utilizar la búsqueda por interpolación

Código de wikipedia

 /* T must implement the operators -, !=, ==, >=, <= and < such that >=, <=, !=, == and < define a total order on T and such that (tm - tl) * k / (th - tl) is an int between 0 and k (inclusive) for any tl, tm, th in T with tl <= tm <= th, tl != th. arr must be sorted according to this ordering. \returns An index i such that arr[i] == key or -1 if there is no i that satisfies this. */ template <typename T> int interpolation_search(T arr[], int size, T key) { int low = 0; int high = size - 1; int mid; while ((arr[high] != arr[low]) && (key >= arr[low]) && (key <= arr[high])) { mid = low + ((key - arr[low]) * (high - low) / (arr[high] - arr[low])); if (arr[mid] < key) low = mid + 1; else if (key < arr[mid]) high = mid - 1; else return mid; } if (key == arr[low]) return low ; else return -1; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!