Cod sursa(job #2530613)

Utilizator stefzahZaharia Stefan Tudor stefzah Data 24 ianuarie 2020 23:31:44
Problema Statistici de ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("sdo.in");
ofstream fout("sdo.out");
int aux, N, a[3000005], poz;
void Quicksort(int st, int dr)
{
    int x = a[(st + dr)/2], i, j;
    i = st;
    j = dr;

    while(i<=j)
   {while ( (i < dr) && (a[i] < x) ) i++;
    while ( (j > st) && (a[j] > x) ) j--;
    if ( i <= j ) {
      aux = a[i];
      a[i] = a[j];
      a[j] = aux;
      i++;j--;
                  }
   }
   //for(int k=1;k<=N;k++)
      //cout<<a[k]<<" ";
    if(st < j && poz>=st && poz<=j)
        Quicksort(st, j);
    if(dr > i && poz>=i && poz <=dr)
        Quicksort(i, dr);
}
int main()
{
    int i;
    fin>>N>>poz;
    for(i=1; i <= N; i++)
        fin>>a[i];
    Quicksort(1,N);
        fout<<a[poz];
}