Cod sursa(job #1279125)

Utilizator thinkphpAdrian Statescu thinkphp Data 29 noiembrie 2014 20:07:53
Problema Sortare prin comparare Scor 40
Compilator c Status done
Runda Arhiva educationala Marime 1.17 kb
/**
 *   @Description Shell Sort for 40 points
 *   @License     MIT
 */
#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();

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) {

                        arr[ i + dis] = arr[ i ]; 
                }

                arr[ i + dis] = aux;
            }
     }
};

void write() {

     uint i;

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

     fclose( stdout );
};