What is wrong with my sorting algorithm?
<p>I am a beginner programmer and I've been trying to create my own sorting algorithm in Python, I don't understand why it outputs only some of the numbers that were present in the input. I put debug prints everywhere to understand the problem but still got nothing. The code should find and move to the final array the biggest number of the input array, and do that until the input array it's empty, but it seems to stop at some point. There was a person with a <a href="http://stackoverflow.com/questions/29234706/what-is-wrong-with-my-sorting-function">similar problem</a> but the solution did not apply to me as well. This is the code:</p>

<pre><code>array = [3, 6, 25, 4, 5, 24, 7, 15, 5, 2, 0, 8, 1]  #just random numbers
output = []
while(len(array) &gt; 0):
    maximum = 0
    for x in array:
        maximum = max(maximum, x)
    output.append(maximum)
    tempArray = []
    for x in array:
        temp = array.pop()
        if(temp &lt; maximum):
            tempArray.append(temp)
    array = tempArray
print(output)
</code></pre>

<p>To diagnose, put some debug prints in the loop, such as <code>print(output, array)</code> at the end of the outer loop.  And maybe more in the inner loop.  After seeing the problem (removing two things from array each inner iteration, this works.</p>

<pre><code>array = [3, 6, 25, 4, 5, 24, 7, 15, 5, 2, 0, 8, 1]  #just random numbers
output = []
while(array):
    maximum = 0
    for x in array:
        maximum = max(maximum, x)
    output.append(maximum)
    tempArray = []
    for x in array:
        if(x &lt; maximum):
            tempArray.append(x)
    array = tempArray
print(output)
</code></pre>

<p>There are, of course, easier and better ways to delete the max from array, and only delete one copy of max instead of all.</p>

python algorithm sorting