insertion sorts...

python_newbie serbulentu at gmail.com
Mon Jun 23 20:52:48 CEST 2008


I don't know this list is the right place for newbie questions. I try
to implement insertion sort in pyhton. At first code there is no
problem. But the second one ( i code it in the same pattern i think )
doesn't work. Any ideas ?

------------------------------------------------------------
def insertion_sort(aList):

    for i in range(len(aList)):
        for j in range(i):
            if aList[i] < aList[j]:
                aList.insert(j,aList[i])
                del aList[i+1]


if __name__ == "__main__":

    MyList = [7,3,5,19,8,2,9,4,15,6,8,3,19]
    insertion_sort(MyList)
    print MyList
-------------------------------------------------------------

def insertion_sort(aList):

    for iterator in aList:
        for number in range(aList.index(iterator)):
            if iterator < number:
                aList.insert(aList.index(number),iterator)
                del aList[aList.index(iterator)+1]

if __name__ == "__main__":

    MyList = [7,3,5,19,8,2,9,4,15,6,8,3,19]
    insertion_sort(MyList)
    print MyList



More information about the Python-list mailing list