Skip to content

Instantly share code, notes, and snippets.

@CrysoK
Last active August 17, 2021 04:27
Show Gist options
  • Save CrysoK/7499a1022c45a8420660909e64f9dc4b to your computer and use it in GitHub Desktop.
Save CrysoK/7499a1022c45a8420660909e64f9dc4b to your computer and use it in GitHub Desktop.
Implementación en C del método de ordenamiento por intercambio directo ("burbuja") inspirado en el pseudocódigo de https://visualgo.net/en/sorting.
void bubbleSort(int arreglo[], int elementos) {
int yaOrdenados = 0;
int huboIntercambio;
int primeroSinOrdenar = 0;
do {
huboIntercambio = 0;
int i = primeroSinOrdenar;
primeroSinOrdenar++;
while(i < elementos - yaOrdenados - 1) {
if(arreglo[i] > arreglo[i + 1]) {
int aux = arreglo[i];
arreglo[i] = arreglo[i + 1];
arreglo[i + 1] = aux;
if(huboIntercambio == 0) primeroSinOrdenar = i > 0 ? i - 1 : 0;
huboIntercambio = 1;
}
i++;
}
yaOrdenados++;
} while(huboIntercambio == 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment