Cod sursa(job #498868)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 7 noiembrie 2010 15:43:42
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<cstdio>
#include <ctime>
using namespace std;

struct NodLSI
{
	int inf;
	struct NodLSI* next;
};

typedef NodLSI* LSI;

LSI prim;

void reads();
void write();

int main()
{
	freopen("algsort.in","r",stdin);
	freopen("algsort.out","w",stdout);
	reads();
	write();
	return 0;
}

void reads()
{
	int n,x;
	LSI p,i;
	scanf("%ld",&n);
	--n;
	p=new NodLSI;
	scanf("%ld",&p->inf);
	p->next=NULL;
	prim=p;
	while (n)
	{
		scanf("%ld",&x);
		--n;
		p=new NodLSI;
		p->inf=x;
		if (p->inf<prim->inf)
		{
			p->next=prim;
			prim=p;
		}
		else 
		{
			for (i=prim;i;i=i->next)
			{
				if (i->next==NULL) break;
				if (i->next->inf>=p->inf) break;
			}
			p->next=i->next;
			i->next=p;
		}
	}
}

void write()
{
	LSI i;
	for (i=prim;i;i=i->next)
		printf("%ld ",i->inf);
}