Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. void swap(int *xp, int *yp)
  7. {
  8. int temp = *xp;
  9. *xp = *yp;
  10. *yp = temp;
  11. }
  12.  
  13. void heapify(int arr[], int n, int i)
  14. {
  15. int largest = i;
  16. int l = 2*i + 1;
  17. int r = 2*i + 2;
  18.  
  19. if (l < n && arr[l] > arr[largest])
  20. largest = l;
  21.  
  22. if (r < n && arr[r] > arr[largest])
  23. largest = r;
  24.  
  25. if (largest != i)
  26. {
  27. swap(&arr[i], &arr[largest]);
  28.  
  29. heapify(arr, n, largest);
  30. }
  31. }
  32.  
  33. void heapSort(int arr[], int n)
  34. {
  35. for (int i = n / 2 - 1; i >= 0; i--)
  36. heapify(arr, n, i);
  37.  
  38. for (int i=n-1; i>=0; i--)
  39. {
  40. swap(&arr[0], &arr[i]);
  41.  
  42. heapify(arr, i, 0);
  43. }
  44. }
  45.  
  46. void countSort(int arr[],int n)
  47. {
  48.  
  49. int output[n], count[n+1], i;
  50.  
  51. for(i = 0; arr[i]; ++i)
  52. ++count[arr[i]];
  53.  
  54. for (i = 1; i <= n; ++i)
  55. count[i] += count[i-1];
  56.  
  57. for (i = 0; arr[i]; ++i)
  58. {
  59. output[count[arr[i]]-1] = arr[i];
  60. --count[arr[i]];
  61. }
  62.  
  63. for (i = 0; arr[i]; ++i)
  64. arr[i] = output[i];
  65. }
  66.  
  67. int main(void) {
  68. int m;
  69. //clock_t czasStart,czasStop,Czas;
  70. m=100;
  71. int numbers[m];
  72. for (int i=0; i<m; i++){
  73. numbers[i]=(rand() % m);
  74. }
  75.  
  76. //czasStart = clock();
  77. //czasStop = clock();
  78. //Czas=czasStop - czasStart;
  79. //printf("%d\n",CLOCKS_PER_SEC); milion jednostek na sekunde
  80.  
  81. heapSort(numbers,m-1);
  82. countSort(numbers,m-1);
  83.  
  84. for (int i=0; i<m; i++){
  85. printf("%d ",numbers[i]);
  86. }
  87.  
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement