allekco

Piramidal

Oct 17th, 2019
120
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <stddef.h>
  6. #include <time.h>
  7.  
  8. int randomazer (int max, int min){
  9. float random;
  10. random = rand();
  11. random = (random / RAND_MAX) * (max-min) + min;
  12. return((int)random);
  13. }
  14.  
  15. void swap (int *a1, int *a2){
  16. int tmp = *a1;
  17. *a1 = *a2;
  18. *a2 = tmp;
  19. }
  20.  
  21. void Max_Heapify (int *A, int i, int N){
  22. int l, r, largest = i;
  23. l = 2*i + 1;
  24. r = 2*i + 2;
  25.  
  26. if ((l < N) && (A[l] > A[largest])){
  27. largest = l;
  28. }
  29. else{
  30. largest = i;
  31. }
  32. if ((r < N) && (A[r] > A[largest])){
  33. largest = r;
  34. }
  35.  
  36. if(largest != i){
  37. swap (&A[i], &A[largest]);
  38. Max_Heapify (A, largest, N);
  39. }
  40. }
  41.  
  42. void Build_Max_Heap (int *A, int N){
  43. int i;
  44. for (i = N/2-1; i >= 0; i--){
  45. Max_Heapify (A, i, N);
  46. }
  47. }
  48.  
  49. void Heapsort (int *A, int N){
  50. int i;
  51. Build_Max_Heap (A, N);
  52. for (i = N-1; i >= 0; i--){
  53. swap (&A[0], &A[i]);
  54. Max_Heapify (A, 0, i);
  55. }
  56. }
  57.  
  58.  
  59. int main (void){
  60. srand(time(NULL));
  61. int N, i;
  62. scanf ("%d", &N);
  63. int A[N+1];
  64. for (i=0 ; i<N ; i++){
  65. A[i] = randomazer (0, 100);
  66. //printf ("%d ", A[i]);
  67. }
  68. printf ("\n");
  69.  
  70. clock_t start = clock ( ) ;
  71. Heapsort (A, N);
  72. clock_t end= clock ( ) ;
  73. float seconds = ( float ) ( end - start ) / CLOCKS_PER_SEC;
  74.  
  75. //for (i=0 ; i<N ; i++){
  76. //printf ("%d ", A[i]);
  77. //}
  78. printf (" \n");
  79. printf ("%f ", seconds); // 100000(200000) - 0,016,
  80.  
  81. return 0;
  82. }
RAW Paste Data