目前實現了氣泡排序、選擇排序、插入排序、希爾排序、歸併排序和快速排序等常見演算法。
1. 氣泡排序
通過兩層迴圈實現排序,每輪內迴圈實現外層子串行中最大/最小值的浮出。
def
bubblesort
(nums:
list):
for i in
range
(len
(nums)-1
):for j in
range
(i+1
,len
(nums)):
if nums[j]
< nums[i]
: nums[i]
, nums[j]
= nums[j]
, nums[i]
return nums
# 一種優化方案:若某輪內迴圈詞序不變提前退出
defbubblesort2
(nums:
list):
exchange =
true
for i in
range
(len
(nums)-1
):ifnot exchange:
return nums
exchange =
false
for j in
range
(i+1
,len
(nums)):
if nums[j]
< nums[i]
: exchange =
true
nums[i]
, nums[j]
= nums[j]
, nums[i]
return nums
2. 選擇排序
氣泡排序的優化,迴避了每兩個數字對比後的交換過程,在每輪內迴圈中,只需記錄下子串行中最大值的位置,迴圈結束後再交換一次即可。
def
selectionsort
(nums:
list):
for i in
range
(len
(nums)-1
):smallindex = i
for j in
range
(i+1
,len
(nums)):
if nums[j]
< nums[smallindex]
: smallindex = j
nums[i]
, nums[smallindex]
= nums[smallindex]
, nums[i]
return nums
3. 插入排序
效仿摸牌的插入過程,在實現時並未直接從空列表開始增長,而是在原有序列上進行臨接位置的騰挪。
def
insertsort
(nums:
list):
for i in
range(1
,len
(nums)):
value = nums[i]
index = i
while index >
0and nums[index-1]
> value:
nums[index]
= nums[index-1]
index -=
1if index != i:
nums[index]
= value
return nums
4. 希爾排序
插入排序的優化,通過在分段子序列上不斷的進行插入排序,使得整個序列不斷有序。
def
shellsort
(nums:
list):
sublistcount =
len(nums)//2
while sublistcount >0:
for startpositon in
range
(sublistcount)
: gapinsertsort(nums, startpositon, sublistcount)
print
("after increment of size {}, the list is {}."
.format
(sublistcount, nums)
) sublistcount //=
2def
gapinsertsort
(alist, start, gap)
:for i in
range
(start+gap,
len(alist)
, gap)
: currentvalue = alist[i]
while i >
0and alist[i-gap]
> currentvalue:
alist[i]
= alist[i-gap]
i -= gap
alist[i]
= currentvalue
5. 歸併排序
利用分治思想,實現序列排序結果和一對子串行排序結果間的遞迴操作。注意在一對子串行排序的對比過程中,需要分別採用乙個動態指標。
def
mergesort
(nums:
list):
iflen
(nums)
>1:
mid_index =
len(nums)//2
left_nums = nums[
:mid_index]
# 切片為deep_copy
right_nums = nums[mid_index:
] mergesort(left_nums)
mergesort(right_nums)
i, j =0,
0while i <
len(left_nums)
and j <
len(right_nums)
:if left_nums[i]
< right_nums[j]
: nums[i+j]
= left_nums[i]
i +=
1else
: nums[i+j]
= right_nums[j]
j +=
1if i ==
len(left_nums)
: nums[i+j:
]= right_nums[j:
]if j ==
len(right_nums)
: nums[i+j:
]= left_nums[i:
]
6. 快速排序
同樣利用遞迴思想,每輪找到樞軸值的排序索引,並據此將序列分為大於樞軸值的子串行和小於樞紐值的子串行。
def
quicksort
(nums:
list):
quicksortiter(nums,0,
len(nums)-1
)def
quicksortiter
(nums, first, last)
:if first < last:
left_marker = first+
1 right_marker = last
pivot = nums[first]
while left_marker < right_marker:
while nums[left_marker]
<= pivot and left_marker <= right_marker and left_marker < last:
left_marker +=
1while nums[right_marker]
>= pivot and left_marker <= right_marker and right_marker >0:
right_marker -=
1if left_marker < right_marker:
# 防止left_marker和right_marker交錯時發生錯誤交換
nums[left_marker]
, nums[right_marker]
= nums[right_marker]
, nums[left_marker]
if nums[right_marker]
< pivot:
nums[right_marker]
, nums[first]
= pivot, nums[right_marker]
quicksortiter(nums, first, right_marker-1)
quicksortiter(nums, right_marker+
1, last)
用JS實現常見排序演算法
嘗試用js實現了幾種常見的排序演算法 插入排序 交換排序 選擇排序 插入排序 直接插入排序 array.prototype.insertsort function this j 1 temp return this console.log 12,15,9,20,6,31,24,48,2,98,100...
python 實現常見排序演算法
coding utf 8 bubble sort 氣泡排序 時間複雜度最壞為o n2 最優的為n import time def bubble sort alist 氣泡排序 param alist return cur 1 while cur len alist 1 and len alist 1...
常見排序演算法Python實現
氣泡排序 最優時間複雜度 o n 最壞時間複雜度 o n 2 穩定性 穩定 def bubble sort alist 氣泡排序 for j in range len alist 1,0,1 count 0 for i in range j if alist i alist i 1 alist i ...