Cod sursa(job #2217693)

Utilizator Mihaibv13Mihai Stoian Mihaibv13 Data 1 iulie 2018 15:41:24
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <cstdio>
using namespace std;

void wap(int &a,int &b)
{
    int aux;
    aux = a;
    a=b;
    b=aux;
}
int main()
{
    int h[100],n,i,x,v[100],k=0;
    //freopen("algsort.in","r",stdin);
    FILE *f=fopen("algsort.in","r");
    FILE *g=fopen("algsort.out","w");
    // freopen("algsort.out","w",stdout);
    // cin>>n;
    fscanf(f,"%d",&n);
    //cin>>h[1];
    fscanf(f,"%d",&h[1]);
    for(i=2; i<=n; i++)
    {
        //cin>>h[i];
        fscanf(f,"%d",&h[i]);
        int nc=i;
        while(h[nc]<h[nc/2] && nc/2>0)
        {
            wap(h[nc],h[nc/2]);
            nc=nc/2;
        }
    }

    while(n)
    {
        v[++k]=h[1];
        h[1]=h[n];
        n--;
        int nc=1,fisu,ifisu;
        while(1)
        {
            if(h[2*nc]<h[2*nc+1])
                fisu=h[2*nc],ifisu=2*nc;
            else
                fisu=h[2*nc+1],ifisu=2*nc+1;
            if(fisu > h[nc] || ifisu>n )
                break;
            wap(h[ifisu],h[nc]);
            nc=ifisu;

        }

    }






    for(i=1; i<=k; i++)
        fprintf(g,"%d ",v[i]);


    return 0;
}