Python 为什么此算法可以按降序排序数据

Python 为什么此算法可以按降序排序数据,python,sorting,Python,Sorting,我学习python编程,并尝试按降序排序数据。 #下面的sort1已成功排序,但我无法理解为什么会发生这种情况。 而且,data[i],data[data.index(mn)]=data[data.index(mn)],data[i]是可疑点 data = [-1.48, 4.96, 7.84, -4.27, 0.83, 0.31, -0.18, 3.57, 1.48, 5.34, 9.12, 7.98, -0.75, 2.22, -1.16, 6.53,

我学习python编程,并尝试按降序排序数据。 #下面的sort1已成功排序,但我无法理解为什么会发生这种情况。 而且,
data[i],data[data.index(mn)]=data[data.index(mn)],data[i]
是可疑点

data = [-1.48,  4.96,  7.84, -4.27,  0.83,  0.31, -0.18,  3.57,  1.48,  5.34,
         9.12,  7.98, -0.75,  2.22, -1.16,  6.53, -5.38,  1.63, -2.85,  7.89,
        -5.96, -8.23,  8.76, -2.97,  4.57,  5.21,  9.43,  3.12,  6.52,  1.58 ]
#sort1

for i in range(30):
    mn = data[i]
    for j in data:
        if j < mn:
            mn = j
            data[i], data[data.index(mn)] = data[data.index(mn)], data[i]
        else:
            pass
print('ascending order1:')
print(data)

data=[-1.48、4.96、7.84、-4.27、0.83、0.31、-0.18、3.57、1.48、5.34、,
9.12,  7.98, -0.75,  2.22, -1.16,  6.53, -5.38,  1.63, -2.85,  7.89,
-5.96, -8.23,  8.76, -2.97,  4.57,  5.21,  9.43,  3.12,  6.52,  1.58 ]
#sort1
对于范围(30)内的i:
mn=数据[i]
对于数据中的j:
如果j
您的代码中有两个错误:

  • 在循环中最好对范围内的i(len(data))执行
    ,因此如果变量数据的大小发生变化,代码将正常工作
  • 您的代码按降序对数据进行排序,因此您应该打印:
    print('descending order1:')
  • 现在,让我们谈谈算法部分。事实上,您的代码是排序算法冒泡排序的实现,也称为下沉排序。该算法反复遍历列表并比较相邻元素。如果相邻元素的顺序错误(即,如果第一个元素低于第二个元素,则交换相邻元素)。它会一直这样做,直到列表被排序(按降序排列)。你可以得到更多的理解

    代码
    data[i],data[data.index(mn)]=data[data.index(mn)],data[i]
    只是交换的一部分。这是一种交换元素的Python方式。例如:

    a = 5
    b = 15
    a, b = b, a # swap 'elegantly a and b
    print(a) #  display 15
    print(b) # display 5
    
    对守则的评论:

    for i in range(30): # first cursor: steps through the indexes of the list
        mn = data[i]    # assigns the data at index i to the variable mn
        for j in data:  # second cursor: steps through the data of the list
            if j < mn:  # compares adjacent elements
                mn = j  
                data[i], data[data.index(mn)] = data[data.index(mn)], data[i] # swap adjacent elements
            else: # if the first data superior to the second, don't do anything
                pass
    
    对于范围(30)中的i:#第一个光标:逐步遍历列表的索引
    mn=数据[i]#将索引i处的数据分配给变量mn
    对于数据中的j:#第二个光标:逐步遍历列表中的数据
    如果j
    这是插入排序

    您可以将其想象为对项目的流式列表进行排序:

    for i in range(30): # for each item streamed here
        mn = data[i]    # take the new item (if exists new item)
        for j in data:  # find its place in the sorted data, and insert it there:
            if j < mn:  # if found its place, insert it here
                mn = j  
                data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 
    
    实际上,此交换命令忽略将来的交换(当
    data.index(mn)
    大于当前时间
    i
    时)。但是,由于它在时间
    i
    大于
    data.index(mn)
    时工作,因此对于插入排序来说就足够了。这是一个例子:

    # two attempts to swapping x and y: 
    data = ['x', 'y']
    
    # ignored (target of swap is at time i, found position in future!):
    i = 0; mn = 'y' # data.index(mn) == 1
    data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 
    print('ignored swap', data) 
    
    # success (target of swap is at time i, found position in past (before i)):
    i = 1; mn = 'x' # data.index(mn) == 0
    data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 
    print('success swap', data)
    

    这是插入排序,我怀疑
    data[I],data[data.index(mn)]=data[data.index(mn)],data[I]
    交换相邻元素。请参阅检查我的更新回答当取消注释
    数据[i],数据[data.index(mn)]=数据[data.index(mn)]
    时,排序失败。这意味着
    data[i],data[data.index(mn)]=data[data.index(mn)]
    无法交换,但有助于排序?当
    i
    大于
    data.index(mn)
    时,交换工作正常,否则将忽略它,这就足以进行插入排序。
    # two attempts to swapping x and y: 
    data = ['x', 'y']
    
    # ignored (target of swap is at time i, found position in future!):
    i = 0; mn = 'y' # data.index(mn) == 1
    data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 
    print('ignored swap', data) 
    
    # success (target of swap is at time i, found position in past (before i)):
    i = 1; mn = 'x' # data.index(mn) == 0
    data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 
    print('success swap', data)