Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void main()
- {
- int heap[10], i, j, c, root, temp, x=0;
- FILE * czytanie;
- FILE * sorted;
- int liczby [10];
- czytanie = fopen("no_sort.txt", "w+");
- while (feof(czytanie)==0)
- {
- fscanf(czytanie, "%d", &liczby[x]);
- x++;
- }
- fclose(czytanie);
- for (i = 1; i < x; i++)
- {
- c = i;
- do
- {
- root = (c - 1) / 2;
- if (heap[root] < heap[c]) /* to create MAX heap array */
- {
- temp = heap[root];
- heap[root] = heap[c];
- heap[c] = temp;
- }
- c = root;
- } while (c != 0);
- }
- printf("Tablica : ");
- for (i = 0; i < x; i++)
- printf("%d\t ", heap[i]);
- for (j = x - 1; j >= 0; j--)
- {
- temp = heap[0];
- heap[0] = heap[j]; /* swap max element with rightmost leaf element */
- heap[j] = temp;
- root = 0;
- do
- {
- c = 2 * root + 1; /* left node of root element */
- if ((heap[c] < heap[c + 1]) && c < j-1)
- c++;
- if (heap[root]<heap[c] && c<j) /* again rearrange to max heap array */
- {
- temp = heap[root];
- heap[root] = heap[c];
- heap[c] = temp;
- }
- root = c;
- } while (c < j);
- }
- sorted = fopen("with_sort.txt", "w");
- for(i=0;i<x;i++)
- fprintf(sorted, "%d\n", heap[i]);
- fclose(sorted);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement