Cod sursa(job #1737066)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 3 august 2016 11:53:46
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

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

vector <int> v;
int n;

int partition(int low,int high)
{
    int pivot=v[low];
    int i=low-1;
    int j=high+1;
LOOP:
    do
        ++i;
    while (v[i]<pivot);
    do
        --j;
    while (v[j]>pivot);
    if (i>=j)
        return j;
    swap(v[i],v[j]);
    goto LOOP;
}

void quicksort(int low,int high)
{
    int pivot;
    if (low<high)
    {
        pivot=partition(low,high);
        quicksort(low,pivot);
        quicksort(pivot+1,high);
    }
}

void citire()
{
    f>>n;
    v.resize(n);
    for (int i=0; i<n; ++i)
        f>>v[i];
}

inline void afisare()
{
    for (int i=0; i<n; ++i)
        t<<v[i]<<" ";
}

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