亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么這個算法可以按降序對數據進行排序

為什么這個算法可以按降序對數據進行排序

catspeake 2022-06-22 18:04:02
我學習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 ]#sort1for 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:            passprint('ascending order1:')print(data)
查看完整描述

2 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

這是插入排序https://en.wikipedia.org/wiki/Insertion_sort


您可以將其想象為對項目的流式列表進行排序:


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] 

更新


插入排序背后的直覺是,每次獲得新項目時,您都會更新先前排序的列表。因此,您無需擔心未來項目的排序位置。


由于時間之前的數據i是排序的,那么在找到第一個交換之后,所有的項目都會交換,直到它再次到達時間i。


現在,交換命令的問題:


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)


查看完整回答
反對 回復 2022-06-22
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

您的代碼中有 2 個錯誤:


最好在循環中執行for i in range(len(data)),因此如果變量數據的大小發生變化,您的代碼將起作用。

您的代碼按降序對數據進行排序,因此您應該打?。簆rint('descending order1:')

現在,讓我們談談算法部分。實際上,您的代碼是排序算法冒泡排序的實現,也稱為下沉排序。該算法重復遍歷列表并比較相鄰元素。如果它們的順序錯誤(即如果第一個元素低于第二個),它將交換相鄰元素。它這樣做直到列表被排序(按降序排列)。你可以在這里得到更多的了解


代碼data[i], data[data.index(mn)] = data[data.index(mn)], data[i]只是交換部分。這是一種交換元素的 Pythonic 方式。例如:


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


查看完整回答
反對 回復 2022-06-22
  • 2 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號