Cod sursa(job #768925)

Utilizator ion824Ion Ureche ion824 Data 17 iulie 2012 21:47:39
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include<iostream>
#include<stdio.h>
using namespace std;

void Quick(int* t,int st,int dr, int k ) //Sortarea rapida  (!)
 
{     int i=st,j=dr,w,piv=t[(i+j)/2];

   
 do   {
	  while(t[i] < piv) i++; 
	  while(t[j] > piv) j--;
      if(i <= j) {w=t[i]; t[i]=t[j]; t[j]=w; i++; j--;}
      }
  while(j >= i);
 
	if(st < j) {
		if( j - st + 1  >= k ) // daca elementul e in stanga
			Quick(t,st,j,k);
	}
	 if(dr > i) 
	 {
		 if( j - st + 1 < k )// daca elementul e in dreapta
			Quick(t,i,dr,  k - ( j - st  + 1 ));// il scad pe k cu cate elemente sunt in stanga
	 }
 }

int i,v[3000001],n;

int main()
{
	freopen("sdo.in","r",stdin);
	freopen("sdo.out","w",stdout);
	
int k;
scanf("%d %d", &n, &k);

for(i=1;i<=n;i++)
    scanf("%d", &v[i]);
Quick(v,1,n,k);

cout<<v[k];


}