Cod sursa(job #724308)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 26 martie 2012 13:33:32
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 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> v;
int n;

void citire();

template<typename T>
void swap(T &a,T &b)
{
    T aux=a; a=b; b=aux;
}

void qsort(int l,int r)
{
    int i=l,j=r,t=(l+r)/2;

    while(i<=j)
    {
        while(v[i]<v[t]) ++i;
        while(v[j]>v[t]) --j;
        if(i<=j)
        {
            swap(v[i],v[j]);
            ++i;
            --j;
        }
    }
    if(i<r) qsort(i,r);
    if(l<j) qsort(l,j);
}


int main()
{
    citire();
	

	 qsort(1,n);

    ofstream fout("algsort.out");

    for(int i = 1; i<=n; ++i)
        fout << v[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;
	v.push_back("");
	for(int i = 1; i <= n; ++i)
	{
		sin >> _aux;
		v.push_back(_aux);
	}

    fin.close();
}