Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. void main()
  3. {
  4. int heap[10], i, j, c, root, temp, x=0;
  5. FILE * czytanie;
  6. FILE * sorted;
  7. int liczby [10];
  8. czytanie = fopen("no_sort.txt", "w+");
  9. while (feof(czytanie)==0)
  10. {
  11. fscanf(czytanie, "%d", &liczby[x]);
  12. x++;
  13. }
  14. fclose(czytanie);
  15.  
  16. for (i = 1; i < x; i++)
  17. {
  18. c = i;
  19. do
  20. {
  21. root = (c - 1) / 2;
  22. if (heap[root] < heap[c]) /* to create MAX heap array */
  23. {
  24. temp = heap[root];
  25. heap[root] = heap[c];
  26. heap[c] = temp;
  27. }
  28. c = root;
  29. } while (c != 0);
  30. }
  31. printf("Tablica : ");
  32. for (i = 0; i < x; i++)
  33. printf("%d\t ", heap[i]);
  34. for (j = x - 1; j >= 0; j--)
  35. {
  36. temp = heap[0];
  37. heap[0] = heap[j]; /* swap max element with rightmost leaf element */
  38. heap[j] = temp;
  39. root = 0;
  40. do
  41. {
  42. c = 2 * root + 1; /* left node of root element */
  43. if ((heap[c] < heap[c + 1]) && c < j-1)
  44. c++;
  45. if (heap[root]<heap[c] && c<j) /* again rearrange to max heap array */
  46. {
  47. temp = heap[root];
  48. heap[root] = heap[c];
  49. heap[c] = temp;
  50. }
  51. root = c;
  52. } while (c < j);
  53. }
  54.  
  55. sorted = fopen("with_sort.txt", "w");
  56. for(i=0;i<x;i++)
  57. fprintf(sorted, "%d\n", heap[i]);
  58. fclose(sorted);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement