Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <iostream>
  8. #include <thread>
  9. #include <chrono>
  10.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15. //llena toda la cadena con numeros random
  16. void fill(int array[], int N, int min, int max)
  17. {
  18. srand(time(NULL));
  19.  
  20. for (int i = 0; i<N; i++)
  21. {
  22. array[i] = (rand() % (max - min)) + min;
  23. }
  24. }
  25.  
  26. //hace los cambios de un lugar a otro
  27. void swap(int &a, int &b)
  28. {
  29.  
  30. int temp = a;
  31. a = b;
  32. b = temp;
  33. }
  34.  
  35.  
  36.  
  37. //realiza el algoritmo de inserción
  38. void insertion(int array[], int N, int *cambios, int* comparaciones)
  39. {
  40.  
  41. for (int i = 1; i < N; i++) {
  42. for (int x = i - 1; x >= 0; x--) {
  43. (*comparaciones)++;
  44. if (array[x + 1] < array[x]) {
  45. swap(array[x + 1], array[x]);
  46. (*cambios)++;
  47. }
  48. else {
  49. break;
  50. }
  51. }
  52. }
  53.  
  54. }
  55.  
  56. //ordenamiento shell
  57. void shell(int array[], int N, int *cambios, int* comparaciones)
  58. {
  59. //el ciclo se hara hasta que h==0
  60. for (int h = N / 2; h != 0; h /= 2) {
  61. bool noCambios = true;
  62. //si no hubo cambios en la esa fase, el ciclo se cierra.
  63. while (noCambios)
  64. {
  65. noCambios = false;
  66. //este ciclo se hace antes de que se llegue a aun desbordamiento
  67. for (int o = 0; o < h; o++)
  68. {
  69. for (int i = o; i + h < N; i += h)
  70. {
  71. (*comparaciones)++;
  72. if (array[i] > array[i + h]) {
  73. //se realizan los cambios de lugar
  74. swap(array[i], array[i + h]);
  75. (*cambios)++;
  76. if (i - h >= 0) {
  77. i -= (2*h);
  78. }
  79. noCambios = true;
  80.  
  81. }
  82. }
  83. }
  84. }
  85. }
  86.  
  87. }
  88.  
  89. int main()
  90. {
  91. //tamaño del arreglo (se va modificando)
  92. int const x = 10;
  93.  
  94. int totalcambiosS=0;
  95. int toatalcomparacionesS=0;
  96. int totalcambiosI = 0;
  97. int toatalcomparacionesI = 0;
  98. //hace el test de 10 pruebas
  99. for (int i = 0; i < 10; i++) {
  100. int array[x] = {};
  101. int array2[x] = {};
  102. //espera 1 segundo para que las cadenas tengan diferentes numeros en cada intento.
  103. std::this_thread::sleep_for(std::chrono::seconds(1));
  104. //llegna el arreglo
  105. fill(array, x, 0, 50);
  106. //copia el arreglo 1 en el 2
  107. for (int n = 0; n < x; n++) {
  108. array2[n] = array[n];
  109. }
  110. //se aplican los metodos
  111. insertion(array, x,&totalcambiosI,&toatalcomparacionesI);
  112. shell(array2, x, &totalcambiosS, &toatalcomparacionesS);
  113. }
  114.  
  115. cout << "promedio comparaciones en insercion " << ((float)toatalcomparacionesI/10) << endl;
  116. cout << "promedio cambios en insercion " << ((float)totalcambiosI/10 )<< endl;
  117. cout << "promedio comparaciones en sell " << ((float)toatalcomparacionesS / 10) << endl;
  118. cout << "promedio cambios en sell " << ((float)totalcambiosS / 10) << endl;
  119.  
  120. _getch();
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement