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

边栏推荐
- Can C use reflection to assign values to read-only attributes?
- C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
- Map collection inheritance structure
- 输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
- 机械制造企业如何借助ERP系统,做好生产管理?
- 【Day_04 0421】计算糖果
- 【Day_05 0422】连续最大和
- [pytorch] fine tuning technology
- 排序问题:冒泡排序,选择排序,插入排序
- JS date details, string to date
猜你喜欢

Address resolution ARP Protocol

力扣——4. 寻找两个正序数组的中位数

『牛客|每日一题』有效括号序列

If introduced according to the open source framework
![[day_060423] no two](/img/2b/5bcb3e089a3157fe72a50ddb767e63.png)
[day_060423] no two
![[untitled]](/img/42/5e8b62edc0aa289098425b26df2453.jpg)
[untitled]

CONDA virtual environment envs directory is empty

Upgrade appium automation framework to the latest 2.0

Basis of multimodal semantic segmentation

『HarmonyOS』探索HarmonyOS应用
随机推荐
Basis of multimodal semantic segmentation
Leetcode 347. top k high frequency elements
[day_020419] sort subsequence
Why use the static keyword when defining methods
『牛客|每日一题』点击消除
@ConstructorProperties注解理解以及其对应使用方式
[day_070425] legal bracket sequence judgment
【Day_06 0423】把字符串转换成整数
Why the server is stuck
BPG notes (IV)
Find the original root
机械制造企业如何借助ERP系统,做好生产管理?
The number of weeks of Oracle last year and this year, with the start time and end time
[untitled]
English sentence pattern reference exclusive Edition - attributive clause
Nuxt configuration topic switching
[day_020419] inverted string
Swift basic FileManager (file management)
【Day05_0422】C语言选择题
信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)