Search code examples
pythonarrayssortinginsertion

Python: Insertion sort logic is failing but i'm not sure why


I've made a challenge to myself to recreate an insertion sort with out looking up examples of code, just from the pseudocode of " pop Number out array, compares the to the number at the same index point, if Number is smaller than the one at array index point move down the array by minus 1, when at an array index point where the value is small than Number, insert it to at that point," I've re-wrote this thing 4 times now and i'd rather have direction than just googling the answers,

I'm self teaching myself coding and i love limiting myself because i believe it will be more disciplined

he's the code, it seams to work for 1 iteration of the while loop but doesn't go further,

array = [3,2,1,0] gets [2, 1, 0, 3]

testArray = [3,2,1,0]
index = 0
for n in testArray:
    tempIndex = index - 1
    if index > 0:
        if n < testArray[tempIndex]:
                testArray.pop(index)
                while True:
                    if testArray[tempIndex] < n:
                        tempIndex -= 1
                    else:
                        break
                testArray.insert(tempIndex,n)
    index += 1

edit: remove debugger to condense code


Solution

  • I think you are not backtracking properly that's why you face that kind of issue.But I Solve it.See it properly.

    testArray = [3,2,1,0]
    index = 0
    for n in testArray:
        tempIndex = index - 1
        
        print("index:     ",index)
        print("tempIndex: ",tempIndex)
        if index > 0:
            if n < testArray[tempIndex]:
                    testArray.pop(index)
                    while True:
                        print("While loop: ",testArray[tempIndex])
                        if testArray[tempIndex] < n:
                            tempIndex -= 1
                        else:
                            break
                    testArray.insert(tempIndex,n)
                    print("TestArray:-----> ",testArray)
                    count=tempIndex
                    print("count: ",count)
                    while(testArray[count]<testArray[count-1]):
                      #Swap value till the start of the testArray[0]
                      temp = testArray[count]
                      testArray[count]= testArray[count-1]
                      testArray[count-1] = temp
                      print("TestArray:---swap->  ",testArray)
                      if(count != 1):
                        count = count-1
                    print("TestArray: ",testArray)
        index += 1
    

    Use Print Statement a lot to check your logic(Thinking) work exactly fine and code work accordingly :

    index:      0
    tempIndex:  -1
    index:      1
    tempIndex:  0
    While loop:  3
    TestArray:----->  [2, 3, 1, 0]
    count:  0
    TestArray:  [2, 3, 1, 0]
    index:      2
    tempIndex:  1
    While loop:  3
    TestArray:----->  [2, 1, 3, 0]
    count:  1
    TestArray:---swap->   [1, 2, 3, 0]
    TestArray:  [1, 2, 3, 0]
    index:      3
    tempIndex:  2
    While loop:  3
    TestArray:----->  [1, 2, 0, 3]
    count:  2
    TestArray:---swap->   [1, 0, 2, 3]
    TestArray:---swap->   [0, 1, 2, 3]
    TestArray:  [0, 1, 2, 3]