Cod sursa(job #2036590)

Utilizator ruxandramateiMatei Ruxandra ruxandramatei Data 10 octombrie 2017 20:34:00
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>
#define DMAX 500001

using namespace std;

ifstream in("algsort.in");
ofstream out("algsort.out");

int val[DMAX], n;

void citire(){
    in >> n;
    for(int i = 1; i<= n; i++){
        in >> val[i];
    }
}

void quickSort(int st, int dr){
    int i = st, j = dr;
    int pivot = val[(st + dr)/2];
    while(i <= j){
        while(i <= dr && val[i] < pivot){
            i++;
        }
        while( j >= st && val[j] > pivot){
            j--;
        }
        if(i <= j){
            swap(val[i], val[j]);
            i++;
            j--;
        }
    }
    if(i < dr){
        quickSort(i, dr);
    }
    if(j > st){
        quickSort(st, j);
    }
}

void afisare(){
    for(int i = 1; i<=n; i++){
        out << val[i] <<' ';
    }
}

int main() {
    citire();
    quickSort(1, n);
    afisare();
    return 0;
}