Cod sursa(job #544551)

Utilizator crouchHotea Cristian crouch Data 1 martie 2011 19:45:17
Problema Sortare prin comparare Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 0.73 kb
#include<stdio.h>
#include<stdlib.h>
int partitie(int s,int d, int *x){
	int i,j,aux;
	int k = x[s];
	i = s;
	j = d+1;
	while(1) {
		do {
			i++;
		}while(k > x[i]);

		do {
			j--;
		}while(k < x[j]);

		if(i < j){
			aux = x[i];
			x[i] = x[j];
			x[j] = aux;
		}
		else {
			aux = x[s];
			x[s] = x[j];
			x[j] = aux;
			return j;
		}	
	}
}
void qs(int s,int d,int *x) {
	if(s < d) {
		int q = partitie(s, d, x);
		qs(s, q, x);
		qs(q+1, d, x);
	
	}
}
int main(){
	int *x;
	int i,n;
	FILE *s = fopen("algsort.in", "r");
	FILE *d = fopen("algsort.out", "w");
	
	fscanf(s,"%d", &n);
	x = (int *)calloc(n, sizeof(int));
	for(i = 0 ; i < n ; i++)
		fscanf(s,"%d", &x[i]);
	
	qs(0, n-1, x);
	
	for(i = 0 ; i < n ; i++)
		fprintf(d,"%d ",x[i]);
}