当前位置:网站首页>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

边栏推荐
- [C language] address book dynamic version and document version
- C# 可以利用反射给只读属性赋值吗?
- If I want to listen to Jay Chou with you, I want you to listen to my whole youth
- dev treelist 常用用法小结
- 【Day_04 0421】计算糖果
- 将一个正整数分解质因数,要求分解成尽可能小的多个的因数。
- 【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
- 输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
- Alibaba cloud OSS binding custom domain name
- [image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
猜你喜欢

Force deduction 5: Longest palindrome substring

Facing the rebound market, how do we operate? 2020-03-21

09 eth smart contract

C# 可以利用反射给只读属性赋值吗?

Go 的切片与数组
![[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you](/img/57/f263f3f3c40b1440b0cbb58c5e05a5.jpg)
[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you

A tool for quickly switching local host files -- switchhosts

输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序

Code runner for vs code, with more than 40million downloads! Support more than 50 languages

TCP protocol -- message format, connection establishment, reliable transmission, congestion control
随机推荐
Vision Transformer 必读系列之图像分类综述
C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
Yolov6: the fast and accurate target detection framework is open source
【Day_05 0422】连续最大和
Why the server is stuck
【C语言】文件操作
@ConstructorProperties注解理解以及其对应使用方式
Basis of multimodal semantic segmentation
Swift basic FileManager (file management)
Interpretation of TPS motion (cvpr2022) video generation paper
将一个正整数分解质因数,要求分解成尽可能小的多个的因数。
深度学习——CV、CNN、RNN
抖音web端 s_v_web_id 参数生成分析与实现
排序问题:冒泡排序,选择排序,插入排序
Valid bracket sequence of "Niuke | daily question"
Database and the future of open source
白盒测试的概念、目的是什么?及主要方法有哪些?
[day_060423] convert string to integer
Three skills are needed to engage in SAP related work
Go 的通道channel