Cod sursa(job #299726)

Utilizator cristiprgPrigoana Cristian cristiprg Data 6 aprilie 2009 22:35:43
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <cstdio>
#include <cstdlib>
#define DIM 500005

int v[DIM], n;

int compare(const void *a,const void *b)
{
	if ( *(int*) a < *(int*)b)
		return -1;

	else
		if ( *(int*) a > *(int*)b)
			return 1;
		else
			return 0;

}

void bubble_sort()
{
	int i, gata = 0, aux;
	while (!gata)
	{
		gata = 1;
		for (i = 0; i < n - 1; i++)
			if (v[i] > v[i + 1])
			{
				aux = v[i];
				v[i] = v[i + 1];
				v[i + 1] = aux;
				gata = 0;
			}
	}
}



int main()
{
	FILE *f = fopen ("algsort.in", "r");
	fscanf(f, "%d", &n);
	for (int i = 0; i < n; i++)
		fscanf(f, "%d", &v[i]);

	fclose(f);
	qsort(v, n, sizeof(int), compare);
//	bubble_sort();
	f = fopen ("algsort.out", "w");
	for (int i = 0; i < n; i++)
		fprintf(f, "%d ", v[i]);
	fclose(f);

	return 0;
}