当前位置:网站首页>Merge_sort
Merge_sort
2022-07-26 06:36:00 【fiveym】
Merge sort
The meaning and use of merging and sorting
- Let's say that the current list is divided into two sections , How to collectively call it a sequence table

def merge(li, low, mid, high):
i = low
j = mid + 1
ltmp = []
while i<=mid and i<=high: # As long as there are numbers on the left and right
if li[i] < li[j]:
ltmp.append(li[i])
i += 1
else:
ltmp.append(li[j])
j += 1
#while After execution , There must be a few left
while i <= mid:
ltmp.append(li[i])
i += 1
while j <= high:
ltmp.append(li[j])
j += 1
li[low:high+1] = ltmp
li = [2,4,5,7,1,3,6,8]
merge(li, 0, 3, 7)
print(li)
How to use merge
- decompose : The smaller the list, the smaller , Until it's divided into elements
- Termination conditions : An element is ordered
- Merge : Merge two sequential tables , The list is getting bigger
- The time complexity is O(logn), The space complexity is O(n)

def merge_sort(li, low, high):
if low < high:
mid = (low + high) //2
merge_sort(li, low, mid)
merge_sort(li, mid+1, high)
merge(li, low, mid, high)
li = list(range(100))
import random
random.shuffle(li)
print(li)
merge_sort(li, 0, len(li)-1)
print(li)
A small summary of various sorts

边栏推荐
- Resume considerations
- 【Day_06 0423】不要二
- Go 的通道channel
- [day_010418] delete public string
- [fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code
- Downloadutilse tool class without error
- How does the seckill system ensure that the database does not crash and prevent oversold goods
- English sentence pattern reference exclusive Edition - attributive clause
- Convert amount figures to uppercase
- 性能测试包括哪些方面?分类及测试方法有哪些?
猜你喜欢
随机推荐
性能测试包括哪些方面?分类及测试方法有哪些?
Find the original root
Do it yourself smart home: intelligent air conditioning control
『HarmonyOS』探索HarmonyOS应用
A tool for quickly switching local host files -- switchhosts
深度学习——CV、CNN、RNN
Liberg avenue to Jane series
C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
[Hangzhou][15k-20k] medical diagnosis company recruits golang development engineers without overtime! No overtime! No overtime!
快速排序(quick-sort)
The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
【pytorch】微调技术
Facing the rebound market, how do we operate? 2020-03-21
What are the main reasons for server crash
dev treelist 常用用法小结
信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)
[day_050422] statistical palindrome
定义方法时为什么使用static关键字
Force buckle - 3. Longest substring without repeated characters
堆排序(heap-sort)








