Insertion-sort error in Python
<p>I am trying to make an insertion sorting program that sorted a list in ascending order. I'm getting the error:</p>

<pre><code>line 16, in &lt;module&gt; if listNums[x] &lt; listNums[i]:
</code></pre>

<p>For this program: </p>

<pre><code>    listNums = [54, 21, 76, 43, 65, 98, 65, 32, 34, 38]

    x = 1
    i = 0
    Copy = 0

    for z in range (0, len(listNums)):
           if listNums[x] &lt; listNums[i]:
                  Copy = listNums[i]
                  listNums[i] = listNums[x]
                  listNums[x] = Copy
           i = i + 1
           x = x + 1

    print(listNums)
</code></pre>

<p>Chances are it's a bounds error because <code>x</code> eventually increments up to <code>len(listNums)</code> which is 1 beyond the bounds of the zero-indexed array.</p>

<p>Try only iterating through <code>range(0, len(listNums)-1)</code>.</p>

python python-3.x insertion-sort