Cod sursa(job #2608927)

Utilizator DeliaGhergheGherghe Ioana-Delia DeliaGherghe Data 1 mai 2020 21:29:14
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
using namespace std;

int partitia(int v[],int i,int j)
{int aux;
    int val=v[j],ii=i,loc=i-1;
    while (ii<=j)
    {
        while (v[ii]>val)
            ii++;
        loc++;
        aux=v[ii];
        v[ii]=v[loc];
        v[loc]=aux;
        ii++;
    }
    return loc;
}

void Quicksort(int v[],int i,int j)
{if (i<j)
   {

int m=partitia(v,i,j);
    Quicksort(v,i,m-1);
    Quicksort(v,m+1,j);}
}

int main()
{
    ifstream fin("algsort.in");
    ofstream fout("algsort.out");

    int n, i, aux, v[500000];
    fin >> n;
    for (i = 0; i < n; i++)
        fin >> v[i];

    Quicksort(v, 0, n-1);

    for (i = 0; i < n; i++)
        fout << v[i] << ' ';

    fin.close();
    fout.close();

    return 0;
}