dimanche 8 novembre 2020

How to implement quick sort using random pivot in python

So far I have this but when I try to print the result I just get a random list not the organized list, any idea of how can I solve it?

import random
lista = [8,12,3,11,5,9,10,4,15,7]

def particionado(lista):

    pivote = random.choice(lista)
    menores = []
    mayores = []
    
    for i in range(1, len(lista)):
        if lista[i] < pivote:
            menores.append(lista[i])
        else:
            mayores.append(lista[i])
            
    return quicksort(menores), pivote , quicksort(mayores)

def quicksort(lista):
    if len(lista) < 2:
        return lista

    menores, pivote, mayores = particionado(lista)
    return menores + [pivote] + mayores

# result
: quicksort(list)
: [7, 9, 9, 9, 9, 9, 9, 9, 9, 15]



Aucun commentaire:

Enregistrer un commentaire