Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Examen de tesinas para FuDePAN */
- /** Funciones Auxiliares */
- /* Assignandcheck coloca el valor de array[arrindex] en res[place], aumenta arrindex en 1,
- y verifica si se ha llegado al final de array; si es asi, le da el valor -1 a arrindex;
- devuelve el nuevo valor de arrindex */
- int assignandcheck (int arrindex, const int place, const unsigned int size, const int array[], int * res) {
- res[place] = array[arrindex];
- arrindex ++;
- if ((unsigned int)arrindex == size) { /* Si se invoca esta funcion, arrindex >= 0 */
- /* por lo que no hay problemas con castearlo a unsigned */
- arrindex = -1;
- }
- return arrindex;
- }
- /** La funcion requerida en el enunciado */
- unsigned int merge(const int array1[], unsigned int size1, const int array2[], unsigned int size2, int result[])
- {
- int fstindex, sndindex; /* Indices para recorrer el primero y segundo arreglos, respectivamente */
- /* Valen -1 si se llego al final del arreglo correspondiente */
- unsigned int i;
- unsigned int size_result = size1 + size2;
- fstindex = 0;
- sndindex = 0;
- for (i=0; i<size_result; i++){
- if (fstindex != -1 && sndindex != -1) { /* Si todavia no se llego al final de ambos arreglos... */
- if (array1[fstindex] <= array2[sndindex]) {
- fstindex = assignandcheck (fstindex, i, size1, array1, result);
- } else {
- sndindex = assignandcheck (sndindex, i, size2, array2, result);
- }
- } else { /* Si se llego al final de un arreglo pero el ciclo todavia no termino... */
- /* ...entonces todavia falta recorrer parte del otro arreglo */
- if (sndindex == -1) {
- fstindex = assignandcheck (fstindex, i, size1, array1, result);
- } else if (fstindex == -1) {
- sndindex = assignandcheck (sndindex, i, size2, array2, result);
- }
- }
- }
- return size_result;
- }
Add Comment
Please, Sign In to add comment