Cod sursa(job #1708505)

Utilizator champsteauaTrocan Gabriel champsteaua Data 27 mai 2016 10:53:39
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.3 kb
#include<iostream>
#include<fstream>
#include <cstring>
#include <cstdlib>
using namespace std;

ifstream f("algsort.in");
ofstream g("algsort.out");
#define long unsigned long

template<class Type> class Object
{
	Type value;
	long priority;


public:

	Object(){
	};
	template<class Ttype> Object(Ttype value1, long priority1)
	{
		value = value1;
		priority = priority1;
	}
	template<class Ttype> void SetValue(Ttype value1)
	{
		value = value1;
	}
	Type GetValue()
	{
		return value;
	}
	void SetPriority(long priority1)
	{
		priority = priority1;
	}
	long GetPriority()
	{
		return priority;
	}
	Object* operator = (Object *nod)
	{
		this->value = nod->GetValue();
		this->priority = nod->GetPriority();

		return this;
	}

	bool operator > (Object obj)
	{
		if (this->GetPriority() > obj.GetPriority())
			return true;

		return false;
	}


	bool operator <= (Object obj)
	{
		if (this->GetPriority() <= obj.GetPriority())
			return true;

		return false;
	}


};


template <class Type> class Heap
{

    Object<Type> a[500000];

public:



	Object<Type> GetValue(long index)
	{
		return a[index];
	}

	void SetIndex(Object<Type> obj, long index)
	{
		a[index] = obj;
	}






	void max_heapify(long i, long n)
	{
		long j;
		Object<Type> temp;

		temp = a[i];
		j = 2 * i;
		while (j <= n)
		{
			if (j < n && a[j + 1] > a[j])
				j = j + 1;
			if (temp > a[j])
				break;
			else if (temp <= a[j])
			{
				a[j / 2] = a[j];
				j = 2 * j;
			}
		}
		a[j / 2] = temp;
		return;
	}
	void heapsort(long n)
	{
		Object<Type> temp;
		for (long i = n; i >= 2; i--)
		{
			temp = a[i];
			a[i] = a[1];
			a[1] = temp;
			this->max_heapify(1, i - 1);
		}
	}


	void build_maxheap(long n)
	{
		long i;
		for (i = n / 2; i >= 1; i--)
		{
			max_heapify(i, n);
		}
	}


};
int main()
{
  long val;
	long p;

	Object<long> d[500000];
	Heap<long> h;
	long length;


	f>>length;

	for (long  i = 1; i <= length; i++)
	{

		f>>val;
		p=val;
		d[i] = new Object<long>(val,p);
		h.SetIndex(d[i],i);
		h.build_maxheap(length);
	h.heapsort(length);
	}


        for (long  i = 1; i <= length; i++)
        {
            g<<h.GetValue(i).GetValue()<<" ";
        }
f.close();
	return 0;
}