Cod sursa(job #724303)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 26 martie 2012 13:30:18
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#define parent(nod) (nod)/2
#define left_child(nod) (nod)*2
#define right_child(nod) (nod)*2+1
using namespace std;

std::vector<std::string> h;
int n;

void citire();

void shift(int k,int N)
{
    int son;
    do
    {
    	son = 0;
    	if(left_child(k) <= N)
    	{
            son = left_child(k);
            if(right_child(k) <= N && h[son] < h[right_child(k)])
                son = right_child(k);
            if(h[son] <= h[k])
                son = 0;
    	}
    	if(son)
        {
            swap(h[son],h[k]);
            k = son;
        }
    }while(son);
}

int main()
{
    citire();
	

    for(int i = n/2; i > 0; --i)
        shift(i,n);

    for(int i = n; i > 0; --i)
    {
        swap(h[1],h[i]);
        shift(1,i-1);
    }

    ofstream fout("algsort.out");

    for(int i = 1; i<=n; ++i)
        fout << h[i] <<' ';

    fout.close();
    return 0;
}

void citire()
{
	std::string aux;
    ifstream fin("algsort.in");
    
	fin >> n;

	getline(fin,aux);

	getline(fin,aux);

	std::istringstream sin(aux);

	std::string _aux;
	h.push_back("");
	for(int i = 1; i <= n; ++i)
	{
		sin >> _aux;
		h.push_back(_aux);
	}

    fin.close();
}