0
Good afternoon, you guys! Can anyone help me by explaining this code 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 how the Shell Sort algorithm works.
https://pt.wikipedia.org/wiki/Shell_sort
– MagicHat
What difficulty is finding, where exactly is the difficulty?
– MagicHat
Only as an aside is this code C and not C++, something you can even see by the link that @Magichat placed. Try to explain exactly what you don’t notice in the code. Indentation also helps
– Isac
@Isac It’s even C++ code, just note that it added the 'ctime' library that in C would be 'time. h' and that in turn does not work in C only in C++, but I found it strange he use 'printf'...
– user83187
@Khyser This library exists in normal C, just change include to
<time.h>
. Everything else is C, norcout
, norvector
, so pure C– Isac
@Isac Yes, I know, I just wanted to say it was C++ code for the library he used :p
– user83187