Last active
April 12, 2017 06:35
-
-
Save gustavorv86/6b1a484067d85dbe62c4d94847cdd80a to your computer and use it in GitHub Desktop.
Search algorythms in C/C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "search.h" | |
unsigned int linear_search(int* array_items, unsigned int size, int search_item) { | |
unsigned int i; | |
for(i=0; i<size; i++) { | |
if(array_items[i] == search_item) { | |
/* elemento encontrado, devolvemos el indice 'i' */ | |
return i; | |
} | |
} | |
/* elemento no encontrado, devolvemos 'size' */ | |
return size; | |
} | |
unsigned int binary_search(int* array_items, unsigned int size, int search_item) { | |
unsigned int min, max, center; | |
min = 0; /* indice minimo */ | |
max = size-1; /* indice maximo */ | |
while(min <= max) { | |
/* calcular el indice central entre 'min' y 'max' */ | |
center = (max + min) / 2; | |
if(search_item == array_items[center]) { | |
/* elemento encontrado, devolvemos el indice 'center' */ | |
return center; | |
} | |
if(search_item < array_items[center]) { | |
/* el elemento esta por debajo de 'center' */ | |
max = center-1; | |
} | |
if(search_item > array_items[center]) { | |
/* el elemento esta por encima de 'center' */ | |
min = center+1; | |
} | |
} | |
/* elemento no encontrado, devolvemos 'size' */ | |
return size; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LINEAR_SEARCH_H | |
#define LINEAR_SEARCH_H | |
/** | |
* | |
* @param array_items: puntero al array de elementos | |
* @param size: dimension de 'array_items' | |
* @param search_item: elemento a buscar | |
* @return: indice de la posicion del elemento o 'size' si el elemento no esta | |
*/ | |
unsigned int linear_search(int* array_items, unsigned int size, int search_item); | |
unsigned int binary_search(int* array_items, unsigned int size, int search_item); | |
#endif | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "search.h" | |
int main(int argc, char** argv) { | |
int* array_items; | |
int item_search; | |
unsigned int size = 1000; | |
unsigned int i, index; | |
/* reserva de memoria para el array de 'size' elementos */ | |
array_items = malloc(size * sizeof(int)); | |
/* rellenamos el array */ | |
/* los elementos han de estar ordenados para la busqueda binaria */ | |
for(i=0; i<size; i++) { | |
array_items[i] = i+1; | |
} | |
/* elemento a buscar */ | |
item_search = array_items[size/3]; | |
/* busqueda lineal (los elementos no tienen por que estar ordenados) */ | |
index = linear_search(array_items, size, item_search); | |
if(index >= size) { | |
printf("Item not found. \n"); | |
} else { | |
printf("Linear search - Index: %u Item: %d \n", index, array_items[index]); | |
} | |
/* busqueda binaria (los elementos han de estar ordenados) */ | |
index = binary_search(array_items, size, item_search); | |
if(index >= size) { | |
printf("Item not found. \n"); | |
} else { | |
printf("Binary search - Index: %u Item: %d \n", index, array_items[index]); | |
} | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment