简体   繁体   English

运行我的函数时,我的if语句没有运行

[英]When running my function my if statement is not running

I am trying to make a simple bubble sort, and the if statement i'm using to sort the numbers in my array is not running. 我正在尝试进行简单的冒泡排序,而我用来排序数组中的数字的if语句没有运行。 Can anybody help me get this to run? 任何人都可以帮助我让它运行吗?

Here is my code: 这是我的代码:

def Bubble( a ):
    Flag = False
    while not Flag:
        Flag = True
        for i in range(0, len(a), -1):
            if a[i] > a[i+1]: #this if statement isn't running
                a[i], a[i + 1] = a[i + 1], a[i]
                print("hi")
                Flag = False



def main():
    a = GRN(10)
    acopy = a[:]
    Bubble(a)
    acopy.sort()
    print(a==acopy)
    print(a)
    print(acopy)


main()

range(0, len(a), -1) is always an empty list, because the step is negative. range(0, len(a), -1)始终为空列表,因为该步骤为负数。 It is not the if statement to blame, but the for loop. 这不是责备的if语句,而是for循环。 What you need is range(len(a)-1) . 你需要的是range(len(a)-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM