Cod sursa(job #2077537)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 28 noiembrie 2017 10:51:07
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <time.h>

int n, A[500100];

int part(int st, int dr){
	int pivot = A[st + (rand() % (dr - st + 1))];
	
	int idx_min = st, idx_max = dr;
	while (1){
		while(A[idx_min] < pivot) idx_min++;
		while(A[idx_max] > pivot) idx_max--;
		
		if (idx_min >= idx_max) return idx_min;
		
		std::swap(A[idx_min++], A[idx_max--]);
	}
}

void quickSort(int st, int dr){
	if (st < dr){
		int mid = part(st, dr);
		quickSort(st, mid - 1);
		quickSort(mid, dr);
	}
}


int main(){
	srand(time(0));
	freopen("algsort.in", "r", stdin);
	freopen("algsort.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
	quickSort(1, n);
	for (int i = 1; i <= n; i++) printf("%d ", A[i]);
	return 0;
}