Created
August 2, 2020 13:53
-
-
Save hello-alf/78cddba1cd072b129c5b1408f76ea026 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# particion que hace ordenamiento usando el pivote | |
#low es el indice mas bajo | |
#high es el indice mas algo | |
def particion(arr, low, high): | |
i = (low-1) | |
# pivot es el valor medio que necesitamos | |
pivot = arr[high] | |
for j in range(low, high): | |
if arr[j] <= pivot: | |
#paso al siguiente valor | |
i = i+1 | |
#cambio de posiciones | |
arr[i], arr[j] = arr[j], arr[i] | |
arr[i+1], arr[high] = arr[high], arr[i+1] | |
return (i+1) | |
def quickSort(arr, low, high): | |
if low < high: | |
#obtenemos el indice de particion | |
pi = particion(arr, low, high) | |
# hacemos los ordenamientos de la izquierda | |
quickSort(arr, low, pi-1) | |
# hacemos los ordenamientos de la derecha | |
quickSort(arr, pi+1, high) | |
arr = [1992, 1990, 10, 5, 6 ,0 ,1, -10] | |
n = len(arr) | |
quickSort(arr, 0, n-1) | |
print("array ordenado:") | |
for i in range(n): | |
print("%d" %arr[i]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment