Skip to main content

insertionSort should in Python be insertion_sort [1]. Also, there is not much point in returning the input array.

What comes to your implementation, the line if lst[i] < lst[i-1]: does not buy you anything; remove it.

Finally, there is a variant called straight insertion sort that minimizes the number of assignments by the factor of 3:

def straight_insertion_sort(lst):
    for i in range(1, len(lst)):
        save = lst[i]
        j = i
        while j > 0 and lst[j - 1] > save:
            lst[j] = lst[j - 1]
            j -= 1
        lst[j] = save

You can find the demonstration [2]. It looks like this:

insertionSort in 2941 ms.
straight_insertion_sort in 1977 ms.
Algorithms agree: True

References

[1] PEP 8PEP 8
[2] Demo program

Hope that helps.

insertionSort should in Python be insertion_sort [1]. Also, there is not much point in returning the input array.

What comes to your implementation, the line if lst[i] < lst[i-1]: does not buy you anything; remove it.

Finally, there is a variant called straight insertion sort that minimizes the number of assignments by the factor of 3:

def straight_insertion_sort(lst):
    for i in range(1, len(lst)):
        save = lst[i]
        j = i
        while j > 0 and lst[j - 1] > save:
            lst[j] = lst[j - 1]
            j -= 1
        lst[j] = save

You can find the demonstration [2]. It looks like this:

insertionSort in 2941 ms.
straight_insertion_sort in 1977 ms.
Algorithms agree: True

References

[1] PEP 8
[2] Demo program

Hope that helps.

insertionSort should in Python be insertion_sort [1]. Also, there is not much point in returning the input array.

What comes to your implementation, the line if lst[i] < lst[i-1]: does not buy you anything; remove it.

Finally, there is a variant called straight insertion sort that minimizes the number of assignments by the factor of 3:

def straight_insertion_sort(lst):
    for i in range(1, len(lst)):
        save = lst[i]
        j = i
        while j > 0 and lst[j - 1] > save:
            lst[j] = lst[j - 1]
            j -= 1
        lst[j] = save

You can find the demonstration [2]. It looks like this:

insertionSort in 2941 ms.
straight_insertion_sort in 1977 ms.
Algorithms agree: True

References

[1] PEP 8
[2] Demo program

Source Link
coderodde
  • 32.3k
  • 15
  • 79
  • 205

insertionSort should in Python be insertion_sort [1]. Also, there is not much point in returning the input array.

What comes to your implementation, the line if lst[i] < lst[i-1]: does not buy you anything; remove it.

Finally, there is a variant called straight insertion sort that minimizes the number of assignments by the factor of 3:

def straight_insertion_sort(lst):
    for i in range(1, len(lst)):
        save = lst[i]
        j = i
        while j > 0 and lst[j - 1] > save:
            lst[j] = lst[j - 1]
            j -= 1
        lst[j] = save

You can find the demonstration [2]. It looks like this:

insertionSort in 2941 ms.
straight_insertion_sort in 1977 ms.
Algorithms agree: True

References

[1] PEP 8
[2] Demo program

Hope that helps.