Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 30, 2020 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/01445cd35825f46e74c2d418940c65bd to your computer and use it in GitHub Desktop.
Save parzibyte/01445cd35825f46e74c2d418940c65bd to your computer and use it in GitHub Desktop.
void burbuja(char arreglo[CANTIDAD_PALABRAS][MAXIMA_LONGITUD_CADENA])
{
int longitud = CANTIDAD_PALABRAS;
// Útil para hacer intercambio de cadenas
char temporal[MAXIMA_LONGITUD_CADENA];
int x, indiceActual;
for (x = 0; x < longitud; x++)
{
for (indiceActual = 0; indiceActual < longitud - 1;
indiceActual++)
{
int indiceSiguienteElemento = indiceActual + 1;
// Si la cadena es menor que la siguiente (alfabeticamente) entonces intercambiamos
if (strcmp(arreglo[indiceActual], arreglo[indiceSiguienteElemento]) <= 0)
{
// Movemos la cadena actual a la temporal
memcpy(temporal, arreglo[indiceActual], MAXIMA_LONGITUD_CADENA);
// Movemos al actual el siguiente elemento
memcpy(arreglo[indiceActual], arreglo[indiceSiguienteElemento], MAXIMA_LONGITUD_CADENA);
// Y en el siguiente elemento, lo que había antes en el actual pero ahora está en temporal
memcpy(arreglo[indiceSiguienteElemento], temporal, MAXIMA_LONGITUD_CADENA);
}
}
}
// No hay necesidad de devolver nada, pues modificamos al arreglo de manera interna
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment