aeds2/fonte/U4 - Ordenação em memória p.../c/shellsort.h

31 lines
928 B
C

#ifndef SHELLSORT_H
#define SHELLSORT_H
//=============================================================================
#include "geracao.h"
//=============================================================================
void insercaoPorCor(int *array, int n, int cor, int h){
for (int i = (h + cor); i < n; i+=h) {
int tmp = array[i];
int j = i - h;
while ((j >= 0) && (array[j] > tmp)) {
array[j + h] = array[j];
j-=h;
}
array[j + h] = tmp;
}
}
//=============================================================================
void shellsort(int *array, int n) {
int h = 1;
do { h = (h * 3) + 1; } while (h < n);
do {
h /= 3;
for(int cor = 0; cor < h; cor++){
insercaoPorCor(array, n, cor, h);
}
} while (h != 1);
}
//=============================================================================
#endif