Search code examples
python-3.xfunctionbubble-sort

Why this Fuction cannot be called


I have this list that I want to sort based on bubble sort, and there is a function in code (Swap()) is refusing to work. I don't know why. there is the code

score = [92,95,7,5,85,55,789,47,125,3265,88,965,655,3,15,448,0,255,455]
size = len(score)
x = 0
COMPS = size - 1

def swap():
    temp = score[x + 1]
    score[x + 1] = score[x]
    score[x] = temp

# The Sort Array Function

def SortArray():
    y = 0
    while y < COMPS:
        x = 0 
        while x < COMPS:
            if score[x] > score[x + 1]:
                #This function not working.
                swap()
            x += 1
        y += 1

 #Display Array Function

def displayArray():
    x = 0
    while x < size:
        print(score[x])
        x += 1

SortArray()
displayArray()

but inserting the swap() code, thus the code under the swap() and replacing it underneath the SortArray(), below the if condition; just like this:

def SortArray():
    y = 0
    while y < COMPS:
        x = 0 
        while x < COMPS:
            if score[x] > score[x + 1]:

                #This Works
                temp = score[x + 1]
                score[x + 1] = score[x]
                score[x] = temp

            x += 1
        y += 1

then it works, so I want to know why the swap() function doesn't get called under the SortArray()


Solution

  • I want to know why the swap() function doesn't get called under the SortArray()

    Actually, it IS called - which you can check by yourself adding a couple print() calls within or using the step debugger - but it doesn't do what you think it should do, because you're confusing local and global variables.

    In SortArray() you define a local variable named x (it's defined as local because you assign it in the function), and this is obviously the one you expect swap() to use. But in your swap function, you use a variable x which is neither an argument of the function nor assigned within the function (both of which would make it a local variable), so it's resolved as the global x declared above.

    IOW, swap uses the global x why you'd expect it to use the one which is local to SortArray(). This is also why the second version works, since this time it uses the proper variable.

    The solution is to remove the global x and explicitely pass the correct value to swap(), ie:

    def swap(x):
        temp = score[x + 1]
        score[x + 1] = score[x]
        score[x] = temp
    
    def SortArray():
        y = 0
        while y < COMPS:
            x = 0 
            while x < COMPS:
                if score[x] > score[x + 1]:
                    swap(x)
                x += 1
            y += 1
    

    And while you're at it, you should also to the same with score - actually, you should avoid globals as much as possible (and believe me, you can write a lot of code without using globals):

    def swap(score, x):
        temp = score[x + 1]
        score[x + 1] = score[x]
        score[x] = temp
    
    def SortArray(score):
        comps = len(score) - 1
        y = 0
        while y < comps:
            x = 0 
            while x < comps:
                if score[x] > score[x + 1]:
                    swap(score, x)
                x += 1
            y += 1
    
    
    def displayArray(score):
        x = 0
        while x < len(score):
            print(score[x])
            x += 1
    
    if __name__ == "__main__":
        score = [92,95,7,5,85,55,789,47,125,3265,88,965,655,3,15,448,0,255,455]
        SortArray(score)
        displayArray(score)
    

    And now your functions can be used with any list or sequence. They are still totally unpythonic but that's obviously not the point here (python has one of the most optimized sort algorithm builtin anyway).