Pagini recente » Cod sursa (job #3239182) | Cod sursa (job #2536804) | Cod sursa (job #313518) | Cod sursa (job #2124761) | Cod sursa (job #724303)
Cod sursa(job #724303)
#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();
}