Search code examples
pythonlistloopspython-3.6bubble-sort

Python, Lists error: IndexError: list index out of range


I have been working on a program that takes a string and sorts it into alphabetical order (Bubble sort). I made a simple piece of code to start me off but i came across a problem shortly after running it in shell. My code:

aList = ["b", "a", "d", "c"]
compOne = 0
compTwo = 1
sorting = True
while sorting == True:
    print (aList)
    sortingList = []
    sortingList.insert(compOne, aList[compOne])
    sortingList.insert(compTwo, aList[compTwo])
    aList[compOne] = sorted(sortingList)[compOne]
    aList[compTwo] = sorted(sortingList)[compTwo]
    print (aList)
    print("__________________________")
    compOne = compOne + 1
    compTwo = compTwo + 1

The idea i had is it will keep going through the list swapping items until it is in alphabetical order (I have not closed the while loop yet, i will proceed to do this when i have got pass this problem) The output i want:

['a', 'b', 'd', 'c']
__________________________
['a', 'b', 'd', 'c']
__________________________
['a', 'b', 'c', 'd']
__________________________
ETC(Will repeat because i haven't closed the while loop)

The output i get along with the error:

['a', 'b', 'd', 'c']
__________________________
Traceback (most recent call last):
  File "C:/Users/MyName/Desktop/Python Programs/Projects/Bubble Sort/Test File 4.py", line 10, in <module>
    aList[compTwo] = sorted(sortingList)[compTwo]
IndexError: list index out of range

As you can tell it came to the second set of items to be compared and came across this error. For some reason aList[compOne] = sorted(sortingList)[compOne] was fine but aList[compTwo] = sorted(sortingList)[compTwo] wasn't (Suggesting they are similiar code)

I have been investigating this problem for an 1 hour and half and i have not found a solution, if you can tell me the problem and explain in depth why this happened i would greatly appreciate it.(I don't just want the answer but i want an explanation with what i did wrong) Meanwhile i am waiting for a answer and explanation i will look into the problem myself. Thanks. (Written in python 3.6.1)


Solution

  • CompOne starts at 0

    CompTwo starts at 1

    You add 1 to both all the time - until CompOne adresses the last character of the string (which is fine) and CompTwo tries to access the character after the last character of the string -> Indexerror.

    I am unsure if I grasp correctly what you try to do, what boggles me most:

    • you reinstantiate your sortingList on every while-loop-round to an empty list?
    • on each while loop you put char n and 'n+1' into your array at some indexes
    • your sortingList is sorted twice (sorted(.....)) which creates new list twice which is quite unnessecary - you could simply sort it once and store that in a temp var that you acess. Why do you use .insert with an index here, the list is empty beside the two chars you put into it due to the sortedList=[] couple lines before

    You need to heed the length of your input array, CompTwo can not exceed that.