#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void ShellSort(int *tab,int n)
{
int i,j,h;
int bufor;
h = 1;
while(h <= n)
{
h *= 3;
h++;
}
while(h > 0)
{
for(i = h;i < n;i++)
{
j = i;
bufor = tab[j];
while(j >= h && tab[j-h] > bufor)
{
tab[j] = tab[j - h];
j -= h;
}
tab[j] = bufor;
}
h/=3;
}
}
void heapify(int *A,int l,int r)
{
int i,j;
int x;
int isCorrect;
x = A[l];
i = l;
j = 2 * i + 1;
isCorrect = 0;
while(j <= r && !isCorrect)
{
if(j < r &&A[j] < A[j+1])
j++;
if(x < A[j])
{
A[i] = A[j];
i = j;
j = 2 * i + 1;
}
else
isCorrect = 1;
}
A[i] = x;
}
void buildHeap(int *A,int n)
{
int i;
for(i = n/2 - 1;i >= 0;i--)
heapify(A,i,n - 1);
}
void heapSort(int *A,int n)
{
int i;
int x;
buildHeap(A,n);
for(i = n - 1; i >= 1;i--)
{
x = A[0];
A[0] = A[i];
A[i] = x;
heapify(A,0,i - 1);
}
}
int main(void) {
// your code goes here
int k,m,n;
int *A;
char esc;
do
{
printf("Podaj liczbe elementow tablicy \n"); printf("Podaj gorny zakres przedzialu dla elementow tablicy \n"); A
= (int*)malloc(n
*sizeof(int)); for(k = 0;k < n;k++)
for(k = 0;k < n;k++)
heapSort(A,n);
for(k = 0;k < n;k++)
printf("Czy chcesz kontynuowac ? \n"); }
while(esc != 'n');
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCnZvaWQgU2hlbGxTb3J0KGludCAqdGFiLGludCBuKQp7CiAgICBpbnQgaSxqLGg7CiAgICBpbnQgYnVmb3I7CiAgICBoID0gMTsKICAgIHdoaWxlKGggPD0gbikKICAgIHsKICAgICAgICBoICo9IDM7CiAgICAgICAgaCsrOwogICAgfQogICAgd2hpbGUoaCA+IDApCiAgICB7CiAgICAgICAgZm9yKGkgPSBoO2kgPCBuO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGogPSBpOwogICAgICAgICAgICBidWZvciA9IHRhYltqXTsKICAgICAgICAgICAgd2hpbGUoaiA+PSBoICYmIHRhYltqLWhdID4gYnVmb3IpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHRhYltqXSA9IHRhYltqIC0gaF07CiAgICAgICAgICAgICAgICBqIC09IGg7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdGFiW2pdID0gYnVmb3I7CiAgICAgICAgfQogICAgICAgIGgvPTM7CiAgICB9Cn0KCnZvaWQgaGVhcGlmeShpbnQgKkEsaW50IGwsaW50IHIpCnsKICAgIGludCBpLGo7CiAgICBpbnQgeDsKICAgIGludCBpc0NvcnJlY3Q7CiAgICB4ID0gQVtsXTsKICAgIGkgPSBsOwogICAgaiA9IDIgKiBpICsgMTsKICAgIGlzQ29ycmVjdCA9IDA7CiAgICB3aGlsZShqIDw9IHIgJiYgIWlzQ29ycmVjdCkKICAgIHsKICAgICAgICBpZihqIDwgciAmJkFbal0gPCBBW2orMV0pCiAgICAgICAgICAgaisrOwogICAgICAgIGlmKHggPCBBW2pdKQogICAgICAgIHsKICAgICAgICAgICAgQVtpXSA9IEFbal07CiAgICAgICAgICAgIGkgPSBqOwogICAgICAgICAgICBqID0gMiAqIGkgKyAxOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgICAgIGlzQ29ycmVjdCA9IDE7CiAgICB9CiAgICBBW2ldID0geDsKfQoKdm9pZCBidWlsZEhlYXAoaW50ICpBLGludCBuKQp7CiAgICBpbnQgaTsKICAgIGZvcihpID0gbi8yIC0gMTtpID49IDA7aS0tKQogICAgICAgIGhlYXBpZnkoQSxpLG4gLSAxKTsKfQoKdm9pZCBoZWFwU29ydChpbnQgKkEsaW50IG4pCnsKICAgIGludCBpOwogICAgaW50IHg7CiAgICBidWlsZEhlYXAoQSxuKTsKICAgIGZvcihpID0gbiAtIDE7IGkgPj0gMTtpLS0pCiAgIC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