Proyecto de ordenamiento para el curso de Análisis y Diseño de Algoritmos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.2KB

  1. /*
  2. * Copyright 2018 Christopher Cromer
  3. * Copyright 2018 Rodolfo Cuevas
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. *
  9. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. *
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. /**
  18. * Unir los resultados de "dividir y conquistar" de merge sort
  19. * @param array El array a ordenar
  20. * @param temp Un array temporario para trabajar
  21. * @param prev_left El lado izquerda anterior
  22. * @param prev_middle La particion de medio anterior
  23. * @param right El lado derecha
  24. */
  25. void merge(int *array, int *temp, int prev_left, int prev_middle, int right) {
  26. int i = 0;
  27. int left = prev_left;
  28. int middle = prev_middle - 1;
  29. int far_right = right - left + 1;
  30. while (prev_left <= middle && prev_middle <= right) {
  31. if (array[prev_left] < array[prev_middle]) {
  32. temp[i++] = array[prev_left++];
  33. }
  34. else {
  35. temp[i++] = array[prev_middle++];
  36. }
  37. }
  38. while (prev_left <= middle) {
  39. temp[i++] = array[prev_left++];
  40. }
  41. while (prev_middle <= right) {
  42. temp[i++] = array[prev_middle++];
  43. }
  44. for (i = 0; i < far_right; i++) {
  45. array[left + i] = temp[i];
  46. }
  47. }
  48. /**
  49. * Correr el merge sort recursivamente
  50. * @param array El array a ordenar
  51. * @param temp Un array temporario para trabajar
  52. * @param left El lado izquerda a ordenar
  53. * @param right El lado derercha a ordenar
  54. */
  55. void merge_sort_run(int *array, int *temp, int left, int right) {
  56. if (left != right) {
  57. int middle = (left + right) / 2;
  58. merge_sort_run(array, temp, left, middle);
  59. merge_sort_run(array, temp, middle + 1, right);
  60. merge(array, temp, left, middle + 1, right);
  61. }
  62. }
  63. /**
  64. * Usar el algoritmo de merge sort
  65. * @param array El array a ordenar
  66. * @param n El tamaño del array
  67. */
  68. void merge_sort(int *array, int n) {
  69. int *temp = malloc(sizeof(int)* n);
  70. if (temp == NULL) {
  71. fprintf(stderr, "Error: Out of heap space!\n");
  72. exit(5);
  73. }
  74. merge_sort_run(array, temp, 0, n - 1);
  75. free(temp);
  76. }