/* autor: Krzysztof 'frimer' Zmijewski
 *
 * prog: " babel_to_file.c " - Program sortuje liczby metoda bobelkowa znajdujace sie
 * 			w pliku doSort.dat, a wynik sortowania wyswietla na ekranie
 * 			i zapisuje do pliku poSort.dat 
 *
 * Kompilator: gcc 4.3.3
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main (void) {
int i, j, tmp, l;
const int N=20;
int tab[N];

system("clear");		/* instrukcja systemowa czyszczaca ekran */

/* otwieramy pliki */
	FILE *doS, *poS;
		if ((doS=fopen("doSort.dat", "r"))==NULL) {
			perror("blad otwarcia pliku");
			exit(-10);
		} 
		if ((poS=fopen("poSort.dat", "w"))==NULL) {
			perror("blad otwarcia pliku");
			exit(-10);
		}

/* czytamy plik doSort.dat - do sortowania */		
l=0;
	while ((!feof(doS))&l<N) {
		fscanf(doS,"%d",&tab[l]);
	l++;
	}

/* wyswietlamy dane przed posortowaniem */
	printf("\nPrzed posortowaniem: ");
		for (i=0; i<N; i++)
	printf("%d ",tab[i]);
	puts("\n");

/* sortujemy dane */
	for (j=0; j<N-1; j++)
		for (i=0; i<N-1-j; i++) /* -j optimalizuje algorytm */
	if (tab[i]<tab[i+1]) { /* znak < dla malejacego oraz > dla rosnacego sortwania */
	
/* to zamien miejscami */
	tmp = tab[i];
	tab[i] = tab[i+1];
	tab[i+1]=tmp;
	}
		
/* wyswietlamy dane po posortowaniu */
	printf("Po posortowaniu: ");
	for (i=0; i<N; i++) {
		printf("%d ", tab[i]);	/* wyswietlamy wynik na ekran */
		fprintf(poS, "%d ", tab[i]); /* zapisujemy wynik do pliku poSort.dat */
    }
		puts("\n");

/* zamykamy pliki */
	fclose (doS);
	fclose (poS);
return 0;
}


