Cod sursa(job #2669941)

Utilizator teochess2017Togan Teodor-Bogdan teochess2017 Data 8 noiembrie 2020 14:57:28
Problema Sortare prin comparare Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <stdio.h>
#include <stdlib.h>

int v[500000];

void quicksort(int min, int max){
   int b, e, piv, aux;
   b = min;
   e = max;
   piv = v[(max + min) / 2];
   while(piv > v[b]){
      b++;
   }
   while(piv < v[e]){
      e--;
   }
   while(b < e){
      aux = v[b];
      v[b] = v[e];
      v[e] = aux;
      do{
        b++;
      }while(piv > v[b]);
      do{
        e--;
      }while(piv < v[e]);
   }
   if(min < e){
      quicksort(min, e);
   }
   if((e + 1) < max){
      quicksort(e + 1, max);
   }
}

int main()
{
    FILE *fin, *fout;
    int n, i;
    fin = fopen("algsort.in", "r");
    fscanf(fin, "%d", &n);
    for(i = 0; i < n; i++){
      fscanf(fin, "%d", &v[i]);
    }
    fclose(fin);
    quicksort(0, n - 1);
    fout = fopen("algsort.out", "w");
    for(i = 0; i < n; i++){
      fprintf(fout, "%d ", v[i]);
    }
    fclose(fout);
    return 0;
}