下面是我學習Python的關於插值排序的寫法,留戀下:
for j in a(1,a.length):
key = j
i =j-1
while i>0 and a[i]>key:
a[i+1]=a[i]
i=i-1
a[i+1] = key
python的寫法是通過空格來區分代碼塊的,並且使用的是分號。
下面是寫成函數的現實
def insertion_Sort(mydata):
for j in a(1,mydata.length):
key = j
i =j-1
while i>0 and mydata[i]>key:
mydata[i+1]=mydata[i]
i=i-1
mydata[i+1] = key
插值排序就是迭代的過程,當獲得一個裝有數值的數組,當元素的個數爲1個的時候,哪麼一定排好了序,先假定 從 a[:j-1]是排行了序的,當j= len(data)的時候,得到的序列
也一定是排行了的序的,我覺得我的表述還不是很清楚。