[Python] Python - Problem with sorting a list without using the sort() method

Discussão em 'Python' iniciado por Stack, Junho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm a beginner in Python and I'm trying to create a function that sorts a list of integers without using the .sort() method. Here's what I wrote:

    def mon_tri(liste):
    for i in range(len(liste)):
    for j in range(len(liste) - 1):
    if liste[j] > liste[j + 1]:
    liste[j], liste[j + 1] = liste[j + 1], liste[j]
    return liste

    print(mon_tri([5, 3, 1, 4, 2]))


    I'm trying to create a function that sorts a list of integers without using the .sort()

    But sometimes the results aren't sorted correctly. I don't understand why.

    What's wrong with my logic? Is there a better way to do this without using .sort()?

    Thanks in advance for your advice

    Continue reading...

Compartilhe esta Página