猿问

为什么这个算法可以按降序对数据进行排序

我学习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)


catspeake
浏览 118回答 2
2回答

RISEBY

这是插入排序https://en.wikipedia.org/wiki/Insertion_sort您可以将其想象为对项目的流式列表进行排序:for i in range(30): # for each item streamed here&nbsp; &nbsp; mn = data[i]&nbsp; &nbsp; # take the new item (if exists new item)&nbsp; &nbsp; for j in data:&nbsp; # find its place in the sorted data, and insert it there:&nbsp; &nbsp; &nbsp; &nbsp; if j < mn:&nbsp; # if found its place, insert it here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mn = j&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i], data[data.index(mn)] = data[data.index(mn)], data[i]&nbsp;更新插入排序背后的直觉是,每次获得新项目时,您都会更新先前排序的列表。因此,您无需担心未来项目的排序位置。由于时间之前的数据i是排序的,那么在找到第一个交换之后,所有的项目都会交换,直到它再次到达时间i。现在,交换命令的问题:data[i], data[data.index(mn)] = data[data.index(mn)], data[i]&nbsp;实际上,这个交换命令会忽略未来的交换(何时data.index(mn)大于当前时间i)。但是,由于它在时间i大于时起作用data.index(mn),因此对于插入排序来说已经足够了。这是一个例子:# two attempts to swapping x and y:&nbsp;data = ['x', 'y']# ignored (target of swap is at time i, found position in future!):i = 0; mn = 'y' # data.index(mn) == 1data[i], data[data.index(mn)] = data[data.index(mn)], data[i]&nbsp;print('ignored swap', data)&nbsp;# success (target of swap is at time i, found position in past (before i)):i = 1; mn = 'x' # data.index(mn) == 0data[i], data[data.index(mn)] = data[data.index(mn)], data[i]&nbsp;print('success swap', data)

ITMISS

您的代码中有 2 个错误:最好在循环中执行for i in range(len(data)),因此如果变量数据的大小发生变化,您的代码将起作用。您的代码按降序对数据进行排序,因此您应该打印:print('descending order1:')现在,让我们谈谈算法部分。实际上,您的代码是排序算法冒泡排序的实现,也称为下沉排序。该算法重复遍历列表并比较相邻元素。如果它们的顺序错误(即如果第一个元素低于第二个),它将交换相邻元素。它这样做直到列表被排序(按降序排列)。你可以在这里得到更多的了解代码data[i], data[data.index(mn)] = data[data.index(mn)], data[i]只是交换部分。这是一种交换元素的 Pythonic 方式。例如:a = 5b = 15a, b = b, a # swap 'elegantly a and bprint(a) #&nbsp; display 15print(b) # display 5代码评论:for i in range(30): # first cursor: steps through the indexes of the list&nbsp; &nbsp; mn = data[i]&nbsp; &nbsp; # assigns the data at index i to the variable mn&nbsp; &nbsp; for j in data:&nbsp; # second cursor: steps through the data of the list&nbsp; &nbsp; &nbsp; &nbsp; if j < mn:&nbsp; # compares adjacent elements&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mn = j&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i], data[data.index(mn)] = data[data.index(mn)], data[i] # swap adjacent elements&nbsp; &nbsp; &nbsp; &nbsp; else: # if the first data superior to the second, don't do anything&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass
随时随地看视频慕课网APP

相关分类

Python
我要回答