2
#include <iostream> #include <cstdlib> #define TAM 10 using namespace std; void shellSort(int vet[], int tamanho){ int i , j , valor, gap = 1; do { gap = 2 * gap + 1; }while(gap > tamanho); do { gap = gap / 2; for (i=0; i < tamanho; i++ ){ valor = vet[i]; j = i - gap; while ((j >= 0) && (valor > vet[j])){ vet[j+gap] =vet[j]; j = j - gap; } vet[j+gap] = valor; } }while(gap > 1); } int main(int argc, char* argv[]){ int a[50] = {12,8,15,7,10,11,24,23,25,18}; int i; for(i=0;i<TAM;i++){

shellsort decrescente

Embed Size (px)

Citation preview

#include <iostream>

#include <cstdlib>

#define TAM 10

using namespace std;

void shellSort(int vet[], int tamanho){

int i , j , valor, gap = 1;

do {

gap = 2 * gap + 1;

}while(gap > tamanho);

do {

gap = gap / 2;

for (i=0; i < tamanho; i++ ){

valor = vet[i];

j = i - gap;

while ((j >= 0) && (valor > vet[j])){

vet[j+gap] =vet[j];

j = j - gap;

} vet[j+gap] = valor;

}

}while(gap > 1);

}

int main(int argc, char* argv[]){

int a[50] = {12,8,15,7,10,11,24,23,25,18};

int i;

for(i=0;i<TAM;i++){

cout << a[i] << " ";

}cout << "\n\nChamando ordenacao...\n\n";

shellSort(a,TAM);

for (i=0;i<TAM;i++) cout << a[i] << " ";

cout << "\n\n";

system("PAUSE");

return 0;

}