-
-
Save parzibyte/34c23ea5604b69669f1e0b0f5889b620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int particion(int[] arreglo, int izquierda, int derecha) | |
{ | |
int pivote = arreglo[izquierda]; | |
while (true) | |
{ | |
/* | |
Acercar los extremos hacia el centro mientras se encuentren elementos ordenados | |
*/ | |
while (arreglo[izquierda] < pivote) | |
{ | |
izquierda++; | |
} | |
while (arreglo[derecha] > pivote) | |
{ | |
derecha--; | |
} | |
// Si los extremos se cruzaron o superaron, entonces toda la porción del arreglo estaba ordenada | |
if (izquierda >= derecha) | |
{ | |
// Regresamos el índice para indicar hasta qué posición el arreglo está en orden | |
return derecha; | |
} | |
else | |
{ | |
// Si no estuvieron ordenados, vamos a hacer el intercambio | |
int temporal = arreglo[izquierda]; | |
arreglo[izquierda] = arreglo[derecha]; | |
arreglo[derecha] = temporal; | |
// Y acercamos en 1 los extremos | |
derecha--; izquierda++; | |
} | |
// El while se repite hasta que izquierda >= derecha | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment