Advertisement
Guest User

Untitled

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