Insertion Sort Advanced Analysis

  • + 0 comments

    Curious why this solution in Python gets a runtime error for about half of the test cases - I am guessing it's something to do with there being an earlier potential exit point for the loop than simply (s < lenA) or that I shouldn't be using functions like index or min in the solution and instead stick to primitive operations. I'm pretty much a novice, so any advice would help:

    def insertionSort(arr):
        # Write your code here
        moves = 0
        s = 0
        a = arr[:]
        lenA = len(arr) - 1
        while s < lenA:
            p = a.index(min(a))
            a.pop(p)
            moves += p
            s += 1
        return moves