Cod sursa(job #1280441)

Utilizator thinkphpAdrian Statescu thinkphp Data 1 decembrie 2014 22:35:30
Problema Sortare prin comparare Scor 40
Compilator c Status done
Runda Arhiva educationala Marime 1.44 kb
/**
 *  @description Cocktail Sort for 40 points.
 *  @License     MIT  
 */

#include <stdio.h>
#define FIN "algsort.in"
#define FOUT "algsort.out"
#define MAXN 500005

int arr[ MAXN ],
    n;

void read();
void write();
void cocktail();
void swap(int,int);
void swap2(int*, int*);

int main() {

    read();
    cocktail(); 
    write();
 
    return(0);
};

void read() {

     int i;

     freopen(FIN, "r", stdin);

     scanf("%d", &n); 
 
     for(i = 0; i < n; i++) scanf("%d", &arr[ i ]);

     fclose( stdin );

};

void cocktail() {

     int i,

         j, 

         swapped;
     
     for(i = 0; i < n / 2; i++) {

         swapped = 0;

         for(j = i; j < n - 1 - i; j++) {

                 if( arr[ j ] > arr[j + 1] ) {

                     swap(j, j + 1);

                     swapped = 1;
                 }
         }

         for(j = n - 2 - i; j > i; j--) {

                 if( arr[ j ] < arr[j - 1] ) {

                     swap(j, j - 1);

                     swapped = 1;
                 }
         }

         if( !swapped )  break;
     }
};

void swap(a, b) {

     int aux;

         aux = arr[ a ] ^ arr[ b ]; 

         arr[ a ] = aux ^ arr[ a ];

         arr[ b ] = aux ^ arr[ b ];
};

void write() {

     int i;

     freopen(FOUT, "w", stdout);
 
     for(i = 0; i < n; i++) printf("%d ", arr[ i ]);

     fclose( stdout );
};