Why use a pointer in this algorithm?

1

When I retreat the code does not work. Why?

And why is the insert emulation algorithm algorithm only suitable for list of small entries, type array and list / p>

 void insert_sort(int *vetor, int TAM){
     int i, j, aux;
     for(i = 1; i < TAM; i++){
         aux = vetor[i];
         for(j = i; (j > 0) && (aux < vetor[j - 1]); j--)
             vetor[j] = vetor[j - 1];
         vetor[j] = aux;
     }
 }
    
asked by anonymous 15.11.2017 / 18:49

1 answer

4

You want to sort a sequence of values, right? If you pass a int is only passing one value.

If you create a sequence of values somewhere you can just pass the address of where the sequence is, and the algorithm can access those values and do what it needs in each of the elements.

You may be asking why not go through the whole sequence. It is probably too large to copy everything from one place to another, which has no need to do this.

You have a question that talks about the practical use of the pointer .

The main problem of the insertion classification algorithm is that it has quadratic complexity (O (N2)) in the worst case. So for only 1000 elements in reverse order he has to do 1 million copies of data operations, that is tragic. But in small volumes this is not shocking and by maintaining reference locale the cache is much more efficient and can be faster than algorithms that make fewer copies but need to compare more or that comparisons cost more, or need more memory.

    
15.11.2017 / 19:18