Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void Heapify(int a, int i, int A[])
  6. {
  7. int l = 2*i;
  8. int r = 2*i + 1;
  9. int largest,temp;
  10. if ((l <= a)&& (A[l] > A[i]))
  11. {
  12. largest = l;
  13. }
  14. else
  15. {
  16. largest = i;
  17. }
  18. if ((r<=a)&&(A[r]>A[largest]))
  19. {
  20. largest = r;
  21. }
  22. if (largest != i)
  23. {
  24. temp = A[i];
  25. A[i] = A[largest];
  26. A[largest] = temp;
  27. Heapify(a,largest,A);
  28. }
  29. }
  30. void BuildHeap(int a, int A[])
  31. {
  32. int i;
  33. for(i = floor(a/2);i>=0;i--)
  34. {
  35. Heapify(a,i,A);
  36. }
  37. }
  38. void HeapSort(int a, int A[])
  39. {
  40. BuildHeap(a,A);
  41. int temp;
  42. int i;
  43. for(i=a;i>=1;i--)
  44. {
  45. temp = A[a];
  46. A[a] = A[0];
  47. A[0] = temp;
  48. a--;
  49. Heapify(a,0,A);
  50. }
  51. }
  52. int main()
  53. {
  54. int liczby=0;
  55. FILE *fp;
  56. int c; //liczba
  57. fp= fopen("kopiec.txt","r");
  58. if(fp==NULL)
  59. {
  60. printf("Taki plik nie istnieje lub jest pusty.");
  61. }else
  62. {
  63. while( c != 0)
  64. {
  65. fscanf(fp,"%d",&c);
  66. liczby++;
  67. }
  68. liczby = liczby - 2;
  69. rewind(fp);
  70. int tab[liczby];
  71. int x;
  72. for(x=0;x<=liczby;x++)
  73. {
  74. fscanf(fp,"%d",&tab[x]);
  75. }
  76. HeapSort(liczby, tab);
  77. FILE *fpsort;
  78. fpsort= fopen("pokopcowane.txt","w+");
  79. for(x=0;x<=liczby;x++)
  80. {
  81. fprintf(fpsort,"%d\n",tab[x]);
  82. }
  83. printf("Wpisano do pliku");
  84. }
  85. fclose(fp);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement