[Python] How does Python insertion sort work?

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    Here's a Python implementation of insertion sort, I tried to follow the values on paper but once the counting variable i gets bigger than len(s) I don't know what to do, how/why does it still run?

    def sort_numbers(s):
    for i in range(1, len(s)):
    val = s
    j = i - 1
    while (j >= 0) and (s[j] > val):
    s[j+1] = s[j]
    j = j - 1
    s[j+1] = val

    def main():
    x = eval(input("Enter numbers to be sorted: "))
    x = list(x)
    sort_numbers(x)
    print(x)

    Continue reading...

Compartilhe esta Página