def
bubblesort
(list):
iflist
!=none:if
len(
list)==
1:pass
else
:for i in
range
(len
(list))
:for j in
range
(len
(list)-
1-i):if
list
[j]>
list
[j+1]:
list
[j],
list
[j+1]=
list
[j+1],
list
[j]if __name__ ==
'__main__'
: list1 =[2
,3,5
,7,8
,9,6
,54,1
,42] bubblesort(list1)
print
(list1)
def
insertsort
(list):
iflist
!=none:if
len(
list)==
1:pass
else
:for i in
range(1
,len
(list))
: temp =
list
[i]for j in
range
(i):
iflist
[j]>
list
[i]:
for k in
range
(i,j,-1
):list
[k]=
list
[k-1
]list
[j]= temp
if __name__ ==
'__main__'
: list1 =[2
,3,5
,7,8
,9,6
,54,1
,42]print
(list1)
insertsort(list1)
print
(list1)
def
partion
(nums,left,right)
: key = nums[left]
while left < right:
# right下標位置開始,向左邊遍歷,查詢不大於基準數的元素
while left < right and nums[right]
>= key:
right -=
1if left < right:
# 找到小於準基數key的元素,然後交換nums[left],nums[right]
nums[left]
,nums[right]
= nums[right]
,nums[left]
else
:# left〉=right 跳出迴圈
break
# left下標位置開始,向右邊遍歷,查詢不小於基準數的元素
while left < right and nums[left]
< key:
left +=
1if left < right:
# 找到比基準數大的元素,然後交換nums[left],nums[right]
nums[right]
,nums[left]
= nums[left]
,nums[right]
else
:# left〉=right 跳出迴圈
break
return left #此時left==right 所以返回right也是可以的
#realize from book "data struct" of author 嚴蔚敏
defquick_sort_standord
(nums,left,right)
:if left < right:
key_index = partion(nums,left,right)
quick_sort_standord(nums,left,key_index)
quick_sort_standord(nums,key_index+
1,right)
num=[2
,4,2
,7,9
,0,3
,4,5
,2,3
,6]quick_sort_standord(num,0,
len(num)-1
)print
(num)
def
selectsort
(list):
iflist
!=none
:for i in
range
(len
(list))
:min
= i for j in
range
(i+1
,len
(list))
:iflist
[min
]>
list
[j]:
min= j
ifmin!= i:
list
[min],
list
[i]=
list
[i],
list
[min
]if __name__ ==
'__main__'
: list1 =[2
,3,5
,7,8
,9,6
,54,1
,42]print
(list1)
selectsort(list1)
print
(list1)
def
shellsort
(data,flag)
:'''
:param data: list, to be sorted
:param flag: 0 -> asc, 1 -> desc
:return: a new sorted list
'''retdata=
for item in data:
count =
len(retdata)
step = count //
2# python3
while step >0:
i =0while i< count:
j = i + step
while j < count:
t = retdata.pop(j)
k = j - step
# asc
if flag ==0:
while k>=0:
if t >= retdata[k]
: retdata.insert(k+
1,t)
break
k = k - step
if k <0:
retdata.insert(
0,t)
# desc
elif flag ==1:
while k >=0:
if t <= retdata[k]
: retdata.insert(k+
1, t)
break
k = k - step
if k <0:
retdata.insert(
0, t)
j = j + step
i = i +
1 step = step//
2return retdata
if __name__ ==
'__main__'
: list1 =[2
,3,5
,7,8
,9,6
,54,1
,42] data = shellsort(list1,0)
print
('asc:'
)print
(data)
data = shellsort(list1,1)
print
('desc:'
)print
(data)
python實現排序
python實現排序 1.氣泡排序 將相鄰的兩個數兩兩比較,每次外迴圈都將最大的數移到最後,氣泡排序是最慢的排序演算法,實際運用中效率最低,當數列為由小到大的有序數列時為最好情況,當由大到小時為為最壞情況。def bubble sort alist count 0 遍歷每一趟比較的次數,每走一趟能得...
排序演算法python實現
先列出一些演算法複雜度的識別符號號的意思,最常用的是o,表示演算法的上屆,如 2n2 o n2 而且有可能是漸進緊確的,意思是g n 乘上乙個常數係數是可以等於f n 的,就是所謂的a b。而o的區別就是非漸進緊確的,如2n o n2 o n2 確實可以作為2n的上屆,不過比較大,就是所謂的a其他符...
Python實現堆排序
usr bin env python coding utf 8 堆排序 class heap object 求給定下標i的父節點下標 defparent self,i if i 2 0 return i 2 1 else return i 2 求給定下標i的左孩子下標 defleft self,i ...