Searching data in Arrays
Linear Search Algoritmo: Comenzando con el primer elemento, se compara cada uno con el valor a buscar y se detiene cuando el valor es hallado o se llega al final del arreglo.
Linear Search Programación en C++
#include<iostream> using namespace std; // Function prototype int busqueda(int[], int, int); const int SIZE = 5; int main() { int pruebas[SIZE] = {87, 75, 98, 100, 82}; int resultados; //Buscar el valor 100 dentro del arreglo resultados = busqueda(pruebas, SIZE, 100); // Si la funcion busqueda regresa el valor -1, no se hallo el valor 100 if(resultados == -1) cout << "Ud. no obtuvo 100 en ningun examen." << endl; else cout << "Ud. obtuvo 100 en la prueba #"; cout << (resultados + 1) << endl; } system("pause"); return 0; int busqueda(int list[],int numElems, int value) int index = 0; int position = -1; bool found = false; while (index < numElems && !found) if (list[index] == value) found = true; position = index; index++; return position;
Binary Search Algoritmo mucho más eficiente que la búsqueda lineal. Requiere que todos los valores en el arreglo esten previamente ordenados. Comienza a realizar la búsqueda con el elemento central, si este contiene el valor buscado, se finaliza el proceso. De lo contrario, si el valor a buscar es menor que el valor central, la búsqueda se concentra en los valores en la primera mitad del arreglo. De otra manera, se concentra en la otra mitad.
Binary Search Programación en C++
#include<iostream> using namespace std; //Function prototype int binarySearch(int[], int, int); const int SIZE = 20; int main() { int idNums[SIZE] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600}; int results; int empID; cout << "Enter the Employee ID you wish to search for: "; cin >> empID; results = binarySearch(idNums, SIZE, empID); if (results == -1) cout << "That number does not exist in the array. " << endl; else cout << "That ID is found at element " << results; cout << " in the array. " << endl; } system("pause"); return 0; int binarySearch(int array[], int size, int value) int first = 0, last = size -1, middle, position = -1; bool found = false; while (!found && first <= last) middle = (first + last) / 2; if (array[middle] == value) found = true; position = middle; else if (array[middle] > value) last = middle - 1; first = middle + 1; return position;
Binary Search Durante la ejecución del programa, entre como prueba el valor 199. Una vez se entre este valor, el programa “llama” a la función binarySearch definida dentro del mismo para llevar a cabo la búsqueda del dato 199 dentro del arreglo.
int binarySearch(int array[], int size, int value) { int first = 0, last = size -1, middle, position = -1; bool found = false; while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) found = true; position = middle; } else if (array[middle] > value) last = middle - 1; else first = middle + 1; return position; Al ejecutarse las instrucciones dentro de esta función, el valor que retornará a la función principal será 4, lo que corresponde a la posición dentro del arreglo donde ubica el dato encontrado.