merge_sort and quick_sort in python
This commit is contained in:
parent
53a608e769
commit
d8f7f90bd3
48
python/12_sorts/merge_sort.py
Normal file
48
python/12_sorts/merge_sort.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
"""
|
||||||
|
Author: Wenru
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
def merge_sort(a: List[int]):
|
||||||
|
_merge_sort_between(a, 0, len(a)-1)
|
||||||
|
|
||||||
|
def _merge_sort_between(a: List[int], low: int, high: int):
|
||||||
|
# The indices are inclusive for both low and high.
|
||||||
|
if low >= high: return
|
||||||
|
mid = low + (high - low)//2
|
||||||
|
_merge_sort_between(a, low, mid)
|
||||||
|
_merge_sort_between(a, mid+1, high)
|
||||||
|
|
||||||
|
_merge(a, low, mid, high)
|
||||||
|
|
||||||
|
def _merge(a: List[int], low: int, mid: int, high: int):
|
||||||
|
# a[low:mid], a[mid+1, high] are sorted.
|
||||||
|
i, j = low, mid+1
|
||||||
|
tmp = []
|
||||||
|
while i <= mid and j <= high:
|
||||||
|
if a[i] <= a[j]:
|
||||||
|
tmp.append(a[i])
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
tmp.append(a[j])
|
||||||
|
j += 1
|
||||||
|
start = i if i <= mid else j
|
||||||
|
end = mid if i <= mid else high
|
||||||
|
tmp.extend(a[start:end+1])
|
||||||
|
a[low:high+1] = tmp
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
a1 = [3, 5, 6, 7, 8]
|
||||||
|
a2 = [2, 2, 2, 2]
|
||||||
|
a3 = [4, 3, 2, 1]
|
||||||
|
a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
|
||||||
|
merge_sort(a1)
|
||||||
|
print(a1)
|
||||||
|
merge_sort(a2)
|
||||||
|
print(a2)
|
||||||
|
merge_sort(a3)
|
||||||
|
print(a3)
|
||||||
|
merge_sort(a4)
|
||||||
|
print(a4)
|
43
python/12_sorts/quick_sort.py
Normal file
43
python/12_sorts/quick_sort.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
"""
|
||||||
|
Author: Wenru
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
import random
|
||||||
|
|
||||||
|
def quick_sort(a: List[int]):
|
||||||
|
_quick_sort_between(a, 0, len(a)-1)
|
||||||
|
|
||||||
|
def _quick_sort_between(a: List[int], low: int, high: int):
|
||||||
|
if low >= high: return
|
||||||
|
# get a random position as the pivot
|
||||||
|
k = random.randint(low, high)
|
||||||
|
a[low], a[k] = a[k], a[low]
|
||||||
|
|
||||||
|
m = _partition(a, low, high) # a[m] is in final position
|
||||||
|
_quick_sort_between(a, low, m-1)
|
||||||
|
_quick_sort_between(a, m+1, high)
|
||||||
|
|
||||||
|
def _partition(a: List[int], low: int, high: int):
|
||||||
|
pivot, j = a[low], low
|
||||||
|
for i in range(low+1, high+1):
|
||||||
|
if a[i] <= pivot:
|
||||||
|
j += 1
|
||||||
|
a[j], a[i] = a[i], a[j] # swap
|
||||||
|
a[low], a[j] = a[j], a[low]
|
||||||
|
return j
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
a1 = [3, 5, 6, 7, 8]
|
||||||
|
a2 = [2, 2, 2, 2]
|
||||||
|
a3 = [4, 3, 2, 1]
|
||||||
|
a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
|
||||||
|
quick_sort(a1)
|
||||||
|
print(a1)
|
||||||
|
quick_sort(a2)
|
||||||
|
print(a2)
|
||||||
|
quick_sort(a3)
|
||||||
|
print(a3)
|
||||||
|
quick_sort(a4)
|
||||||
|
print(a4)
|
Loading…
Reference in New Issue
Block a user