Bifalgorithm post · Computer science one · Sorting Pointers to Elements

Why is it more efficient to sort pointers to structures?

Why is it more efficient to sort pointers to structures? (Computer science one · Sorting Pointers to Elements) — "Recall that it is sometimes preferable to mai…

Ideas in this flowchart

  • Para ordenar estructuras, suele ser preferible mantener un arreglo de punteros a estructuras.
  • La ordenación es un escenario en el que esta preferencia resulta especialmente importante.
  • Al ordenar un arreglo de estructuras, cada intercambio copia la estructura completa de un lugar a otro.
  • El costo de copiar puede ser considerable según el número de bytes de cada estructura.
  • Un arreglo de punteros a estructuras suele ser más eficiente para ordenar.
  • Los intercambios requieren cambiar únicamente los punteros, no las estructuras completas.
  • Intercambiar direcciones de memoria es mucho más barato porque una dirección suele ser menor que la estructura referenciada.
  • Este criterio también se aplica al ordenar estructuras definidas por el usuario.
  • La estructura Student es pequeña porque contiene pocos campos.
  • Las estructuras pequeñas no hacen que la copia sea demasiado problemática, pero las estructuras grandes pueden resultar costosas.
Feed/

Why is it more efficient to sort pointers to structures?

No cover

Channel

Computer science one

1 page

"Recall that it is sometimes preferable to maintain an array of pointers to structures rather than an array of structures. Sorting is a scenario where this is particularly true. When sorting an array of structure elements, the entire structure is copied back and forth as elements are swapped. Depending on the number of bytes of a structure, this can be quite expensive. It is generally more efficient to sort an array of pointers to structures instead. Another case is when we wish to sort user-defined structures. The Student structure presented earlier is “small” in that it only has a few fields. When structures are stored in an array and sorted, there may be many swaps of individual elements which involves a lot of memory copying. If the structures are small this is not too bad, but for “larger” structures this could be potentially expensive. Instead, it may be preferred to have an array of pointers to structures. Swapping elements involves only swapping pointers instead of the entire structure. This is far cheaper as a memory address is likely to be far smaller than the actual structure it points to."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.