Pagini recente » Cod sursa (job #1028515) | Cod sursa (job #2369145) | Cod sursa (job #1689208) | Cod sursa (job #2740440) | Cod sursa (job #1708125)
#include<iostream>
#include<string.h>
#include<fstream>
#include "complex.H"
#include "Numar.h"
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
//const int MAX=100;
template<class Type> class Object
{
Type value;
int priority;
public:
Object(){
};
template<class Ttype> Object(Ttype value1,int priority1)
{
value=value1;
priority=priority1;
}
template<class Ttype> void SetValue(Ttype value1)
{
value = value1;
}
Type GetValue()
{
return value;
}
void SetPriority(int priority1)
{
priority = priority1;
}
int GetPriority()
{
return priority;
}
Object* operator = (Object *nod)
{
this->value = nod->GetValue();
this->priority = nod->GetPriority();
return this;
}
void operator == (Object temp1)
{
Object temp;
temp = *this;
*this = temp1;
temp1 = temp;
}
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;
}
int operator > (string str2)
{
if(strcmp(this->GetValue(),str2) > 0 )
return 1;
return -1;
}
int operator < (string str2)
{
if(strcmp(this->GetValue(),str2) <= 0 )
return 1;
return -1;
}
bool operator <= (Object obj)
{
if(this->GetPriority() <= obj.GetPriority())
return true;
return false;
}
};
template <class Type> class Heap
{
Object<Type> a[100000];
public :
Object<Type> GetValue(int index)
{
return a[index];
}
void SetIndex(Object<Type> obj ,int index)
{
a[index] = obj;
}
int CompareTo(int i ,int j)
{
int rez =0;
Object<Type> firstEle= a[i];
Object<Type> lastEle= a[j];
if (firstEle.GetValue() > lastEle.GetValue())
rez = 1;
else
{
if (firstEle.GetValue() < lastEle.GetValue())
rez = -1;
}
return rez;
}
void max_heapify(int i, int n)
{
int 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( int n)
{
Object<Type> temp;
for (int i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
this->max_heapify(1, i - 1);
}
}
void build_maxheap( int n)
{
int i;
for(i = n/2; i >= 1; i--)
{
max_heapify(i, n);
}
}
void heap_extract_min(int n)
{
if (n<1)
g<<"ERROR UNDERFLOW";
cout<<"Value: "<<a[1].GetValue()<<" Prior: "<<a[1].GetPriority()<<endl;
a[1]= a[n];
n--;
max_heapify(1,n);
}
void heap_extracting(int index,int n)
{
if (n<1)
g<<"ERROR: UNDERFLOW";
cout<<"Value: "<<a[index].GetValue()<<" Prior: "<<a[index].GetPriority()<<endl;
a[index]= a[n];
n--;
max_heapify(1,n);
}
};
int main()
{
int val;
int p;
Object<int> d[100];
Heap<int> h;
int length;
//!Code for InfoArena ALGSORT;
f>>length;
for (int i = 1; i <= length; i++)
{
//cout<<"Enter Value: ";
f>>val;
p=val;
d[i] = new Object<int>(val,p);
h.SetIndex(d[i],i);
}
h.build_maxheap(length);
h.heapsort(length);
for (int i = 1; i <= length; i++)
g<<h.GetValue(i).GetValue()<<" ";
return 0;
//!InfoArena Code done up here instead of the For below
cout<<"Enter no of elements of array";
cin>>length;
for (int i = 1; i <= length; i++)
{
cout<<"Enter Value: ";
cin>>val;
cout<<"Enter Priority: ";
cin>>p;
d[i] = new Object<int>(val,p);
h.SetIndex(d[i],i);
}
h.build_maxheap(length);
h.heapsort(length);
//cout<<"Compare: ";
//cout<<h.CompareTo(1,2);
char op;
do
{
int ch;
cout<<"----------Menu-------------\n\n";
cout<<"\t Press 1 to Insert a new node"<<endl;
cout<<"\t Press 2 for Extracting the first element in Queue"<<endl;
cout<<"\t Press 3 to see the Priority Queue"<<endl;
cout<<"\t Press 4 to compare 2 indexes"<<endl;
cout<<"\t Press 5 to Order by Value"<<endl;
cout<<"\n\n\tEnter your Choice<1,2,3,4,5> ";
cout<<endl;
cin>>ch;
switch(ch)
{
case 1 :{ length++;
cout<<"Enter Value: ";
cin>>val;
cout<<"Enter Priority (KEY): ";
cin>>p;
d[length] = new Object<int>(val,p);
h.SetIndex(d[length],length);
h.build_maxheap(length);
h.heapsort(length);
cout<<endl;
break;
}
case 2 : { cout<<"\n PRIORITY QUEUE \n";
h.heap_extract_min(length);
length--;
h.build_maxheap(length);
h.heapsort(length);
break;
}
case 3:{
for(int i=1;i<=length;i++){
Object<int> temp = h.GetValue(i);
cout<<temp.GetValue()<<" "<<temp.GetPriority();
cout<<endl;
}
cout<<endl;
break;
}
case 4:
{ int index1,index2;
cout<<"Compare: index1: ";
cin>>index1;
cout<<"index2: ";
cin>>index2;
cout<<h.CompareTo(index1,index2);
cout<<endl;
break;
}
case 5:
{ int index,indes;
cout<<"Value order : "<<endl;
Object<int> temp;
while(length>0){
indes=1;
for(int i=2;i<=length;i++){
if(h.CompareTo(indes,i)>0){
indes=i;
} }
index=indes;
h.heap_extracting(index,length);
length--;
h.build_maxheap(length);
h.heapsort(length);
}
cout<<endl;
break;
}
}
cout<<"\n\nDo You Want to Continue <Y/N> ?";
cout<<endl;
cin>>op;
}
while(op=='Y' || op=='y');
return 0;
}