Cod sursa(job #587799)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 5 mai 2011 21:42:56
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#include <algorithm>
FILE *g=fopen("ge.in","r");
FILE *z=fopen("ze.out","w");
using namespace std;
int n,v[500500],o;
void sort(int l,int r)
{
    int i=l,j=r;
    int x=v[(l+r)/2];
    while(i<=j)
    {
        while(v[i]<x)
        i++;
        while(v[j]>x)
        j--;
        if(i<=j)
        {
            swap(v[i],v[j]);
            i++;
            j--;
        }
    }
    if(l<j)
    sort(l,j);
    if(i<r)
    sort(i,r);
}
void binar()
{
    int down=1,high=n,t;
    while(down<=high)
    {
        t=(down+high)/2;
        if(v[t]<o)
        down=t+1;
        else if(v[t]>o)
        high=t-1;
        else
        {
            fprintf(z,"\nElementul %d a fost gasit pe poz %d.",o,t);
            return ;
        }
    }
}
int main()
{
    int i;
    fscanf(g,"%d%d",&n,&o);
    for(i=1;i<=n;i++)
    fscanf(g,"%d",&v[i]);
    sort(1,n);
    for(i=1;i<=n;i++)
    fprintf(z,"%d ",v[i]);
    binar();
    return 0;
}