Guest User

Untitled

a guest
Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. /*
  2. * Tercera Tarea: Ejercicios - Ordenamiento y busqueda
  3. * Pregunta 7 - Ordenamiento por intercalacion
  4. *
  5. * Archivo: mergesort_iterativo.cpp
  6. * Federico Jhoel Salas Gonzalez 201244557
  7. * IC2001 - Estructura de Datos - Grupo 1
  8. * Profesor Armando Arce
  9. * Ingenieria en Computacion
  10. * Escuela de Computacion
  11. * Instituto Tecnologico de Costa Rica
  12. *
  13. * Descripcion de la funcion
  14. *
  15. * La funcion recibe como parametros el puntero a un arreglo de
  16. * strings y la cantidad de elementos en el arreglo. Se ordenan
  17. * los elementos utilizando la tecnica de intercalacion o mergesort,
  18. * sin embargo esta se hace de forma iterativa. Para lograr assumir
  19. * largos que sean potencias de dos se llenan los espacios restantes
  20. * en el arreglo temporal con strings provisionales, en este caso con
  21. * strings que contienen el caracter nulo. La funcion no devuelve una
  22. * copia sino que altera el arreglo original
  23. */
  24.  
  25. #include <iostream>
  26. #include <cmath>
  27. #include "mergesort_iterativo.h"
  28.  
  29. void ordMergeIterativo(std::string * arreglo, int size){
  30.  
  31. // Longitud necesaria (potencia de 2)
  32.  
  33. int tmpSize = pow(2, ceil(log2(size)));
  34.  
  35. // Crear arreglo temporal
  36.  
  37. std::string * tmp = new std::string[tmpSize];
  38.  
  39. // Llenar espacios sobrantes con strings
  40. // que contienen unicamente el caracter nulo
  41.  
  42. for(int pos = size; pos < tmpSize; pos++){
  43. tmp[pos] = "\0";
  44. }
  45.  
  46. // Ordenar el arreglo hasta obtener un grupo de tmpSize
  47.  
  48. for(int groups = 2; groups <= tmpSize; groups *= 2){
  49.  
  50. // Mover elementos del arreglo original al arreglo temporal
  51.  
  52. for(int i = 0; i < size; i++){
  53. tmp[i] = arreglo[i];
  54. }
  55.  
  56. // Regresar elementos al arreglo original ordenando por grupos
  57.  
  58. // Inicio con grupos de dos
  59.  
  60. int current = 0;
  61. int leftIndex = 0;
  62. int rightIndex = groups / 2;
  63. int endLeftGroup = rightIndex;
  64. int endRightGroup = groups;
  65.  
  66. while(current < size){
  67.  
  68. if(rightIndex == endRightGroup ||
  69. std::strcmp(tmp[rightIndex].c_str(), "\0") == 0){
  70.  
  71. if(leftIndex == endLeftGroup){
  72.  
  73. // Continuar con el siguiente par de grupos
  74.  
  75. leftIndex = rightIndex;
  76. rightIndex += groups / 2;
  77. endLeftGroup = rightIndex;
  78. endRightGroup += groups;
  79. continue;
  80. }
  81.  
  82. // Asignar siguiente del grupo izquierdo
  83.  
  84. arreglo[current++] = tmp[leftIndex++];
  85. continue;
  86.  
  87. }
  88.  
  89. if(leftIndex == endLeftGroup){
  90.  
  91. // Asignar siguiente del grupo derecho
  92.  
  93. arreglo[current++] = tmp[rightIndex++];
  94. continue;
  95. }
  96.  
  97. // Asignar el elemento menor
  98.  
  99. if(std::strcmp(tmp[leftIndex].c_str(), tmp[rightIndex].c_str()) < 0){
  100. arreglo[current++] = tmp[leftIndex++];
  101. } else {
  102. arreglo[current++] = tmp[rightIndex++];
  103. }
  104. }
  105. }
  106.  
  107. delete[] tmp; // Liberar memoria del arreglo temporal
  108. }
Add Comment
Please, Sign In to add comment