ShellSort C ++ Algorithm

0

Good afternoon everyone! Can someone help me by explaining this code to me in c ++:

#include < stdlib.h>
#include < stdio.h>
#include < ctime>

void shellSort(int numbers[], int array_size);


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

//seed random number generator
srand((unsigned)time(0)); //Descomente essa linha caso voce queira que o programa gere os numeros aleatorios.

int NUM_ITEMS = 5;
int numbers[5];
int i;

//Descomente o trecho abaixo caso voce queira que o programa gere os numeros aleatorios.
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand();


//numbers[100] = {12,2,6,9,5,7,16,55,32,15}; //Comente esse trecho, caso voce descomente o trecho de codigo acima.

shellSort(numbers, NUM_ITEMS);

printf("Numeros em ordem crescente:\n");
for (i = 0; i < NUM_ITEMS; i++)
printf("%i\n", numbers[i]);

return 0;
}


void shellSort(int numbers[], int array_size)
{
int i, j, increment, temp;

increment = 3;
while (increment > 0)
{
for (i=0; i < array_size; i++)
{
j = i;
temp = numbers[i];
while ((j >= increment) && (numbers[j-increment] > temp))
{
numbers[j] = numbers[j - increment];
j = j - increment;
}
numbers[j] = temp;
}
if (increment/2 != 0)
increment = increment/2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}

I'm having trouble understanding the operation of the Shell Sort algorithm.

    
asked by anonymous 02.12.2017 / 20:08

0 answers