Home > front end >  A index out of range error based on quick sort algo
A index out of range error based on quick sort algo

Time:12-31

I'm making a visual representation tool for the quick sort algorithm, and I'm using a 1 dimensional array that is a random range of index's, filled with a random range of values. I believe the method im using to fill this array at run time is causing the issue, as ive noticed it happens about 50% of the time, but i havent been able to determine any causation/correlation between the value of the indexes, or the length of the array that would return this error:

canvas.create_line(masterArray[x]   20 ,1 ,masterArray[x]  20, masterArray[currValue] * 1   100)
IndexError: list index out of range

I'll attach the code below. My theory is that some of the values that are being filled aren't providing enough information for the line to be drawn, which is : x start, y start, x end, and y end. I cant think of any values with my current system that would not provide this information unfortunately. If anyone has any ideas id really appreciate a nudge in the right direction.

from tkinter import Tk, Canvas, Frame, BOTH, W
import random
import numpy as np
randInt = random.randint(10, 100)
masterArray = [random.randint(10,75) for _ in range(randInt)]
print(randInt)
print(masterArray)

    class Example(Frame):
    
        def __init__(self):
            super().__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.master.title("Lines")
            self.pack(fill=BOTH, expand=1)
            sortedArr =quicksort(masterArray)
    
    
            canvas = Canvas(self)
            ##canvas.create_text(100,100, anchor=W , font ="Purisa", text = len(masterArray))
            for x in masterArray:
                currValue = masterArray[x]
                canvas.create_line(masterArray[x]   20 ,1 ,masterArray[x]  20, masterArray[currValue] * 1   100)
            
            for x in sortedArr:
                currValue = sortedArr[x]
                canvas.create_line(sortedArr[x]   20 ,500,sortedArr[x]  20, 500 - sortedArr[currValue] - 100 )
                ##canvas.create_line(x start,ystart ,x end, y end)
    
            canvas.create_text(125, 250, anchor=W, font="Purisa",text = "The total number of elements is: "   str(len(masterArray)))
            
            ##canvas.create_line(15, 25, 200, 25)
            ##canvas.create_line(300, 35, 300, 200, dash=(4, 2))
           ## canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85)
    
            canvas.pack(fill=BOTH, expand=1)
    
    def quicksort(arr):
        elements = len(arr)
        if elements < 2:
            return arr
        current_pos = 0
    
        for i in range(1, elements):
            if arr[i] <= arr[0]:
                current_pos  = 1
                temp = arr[i]
                arr[i] = arr[current_pos]
                arr[current_pos] = temp
        
        temp = arr[0]
        arr[0] = arr[current_pos]
        arr[current_pos] = temp
        left = quicksort(arr[0:current_pos])
        right = quicksort(arr[current_pos 1:elements])
        arr = left  [arr[current_pos]]   right
        
        
        return arr
    
    def main():
       
        root = Tk()
        ex = Example()
        root.geometry("500x500 300 300")
        root.mainloop()
    
    
    if __name__ == '__main__':
        main()

I've tried playing with the random value arguments to see if anything improves, but haven't had any luck so far. I appreciate any help. Thanks.

CodePudding user response:

Be sure to study what an iterator is and what for does:

Something like this is much better:

for x,currValue in enumerate(masterArray):
    canvas.create_line(x   20 ,1 ,x   20, currValue * 1   100)

for x,currValue in enumerate(sortedArr):
    canvas.create_line(x   20 ,500, x  20, 500 - currValue - 100 )
  • Related