Cod sursa(job #1276971)

Utilizator thinkphpAdrian Statescu thinkphp Data 27 noiembrie 2014 00:21:16
Problema Sortare prin comparare Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.19 kb
#include <stdio.h>
#define FIN "algsort.in"
#define FOUT "algsort.out"
#define MAXN 500005

typedef unsigned int uint;

uint arr[ MAXN ],
     n;

//functions prototype
void read();
void shell();
void write();
void swap(int,int);

int main() {

    read();
    shell();
    write();

  return(0);
};

void read() {

     uint i;

     freopen(FIN, "r", stdin);

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

     fclose( stdin );

};

void shell() {

 int d,
     dis,
     i,
     j,
     aux;

     int dist[ 8 ] = {15, 13, 11, 9, 7, 5, 3, 1};

     for(d = 0; d < 8; d++) {

            dis = dist[ d ];

            for(j = dis; j < n; j++) {

                aux = arr[ j ];

                for(i = j - dis; i >= 0 && arr[ i ] > aux; i -= dis );

                        swap(i, j); 
            }
     }
};

void write() {

     int i;

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

     fclose( stdout );
};

void swap(int x, int y) {

     int xo;

     xo = arr[ x ] ^ arr[ y ];    

     arr[ x ] = xo ^ arr[ x ];

     arr[ y ] = xo ^ arr[ y ]; 
}