aeds2/fonte/u03 Ordenação em memória pr.../c/shellsort.c

28 lines
804 B
C

#include "shellsort.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);
}
//=============================================================================