Cod sursa(job #1815972)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 25 noiembrie 2016 23:10:54
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <iostream>
#include <fstream>
#include <stdlib.h>
#define NMAX 500001
#define BUFF_SIZE 100001

using namespace std;

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

int A[NMAX], n, pos = 0;
char buff[BUFF_SIZE];

void Read(int &a)
{
    while (!isdigit(buff[pos]))
        if (++pos == BUFF_SIZE)
            in.read(buff, BUFF_SIZE), pos = 0;
    a = 0;
    while (isdigit(buff[pos]))
    {
        a = a * 10 + (buff[pos] - '0');
        if (++pos == BUFF_SIZE)
            in.read(buff, BUFF_SIZE), pos = 0;
    }
}

void quicksort(int x, int y)
{
    if (x < y)
    {
        int i = x, j = y;
        int Loc = x;
        while (i < j)
        {
            while (A[Loc] <= A[j] && j != Loc)
                j--;
            if (A[Loc] > A[j])
            {
                swap(A[Loc], A[j]);
                Loc = j;
            }
            while (A[i] <= A[Loc] && i != Loc)
                i++;
            if (A[i] > A[Loc])
            {
                swap(A[Loc], A[i]);
                Loc = i;
            }
        }
        int m = i;
        quicksort(x, m - 1);
        quicksort(m + 1, y);
    }
}

int main()
{
    Read(n);
    for (int i = 1; i <= n; i++)
        Read(A[i]);
    in.close();
    quicksort(1, n);
    for (int i = 1; i <= n; i++)
        out << A[i] << " ";
    out.close();
    return 0;
}