当前位置:网站首页>787. 归并排序
787. 归并排序
2022-08-01 01:22:00 【aJupyter】
Question
给定你一个长度为 n 的整数数列。
请你使用归并排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。
第二行包含 n 个整数(所有整数均在 1∼109 范围内),表示整个数列。
输出格式
输出共一行,包含 n 个整数,表示排好序的数列。
数据范围
1≤n≤100000
输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
Ideas
归并排序
Code
''' 归并排序流程 - 1.确定中间点(l+r>>1) - 2.递归 - 3.合并(双指针) '''
def merge_sort(q,l,r):
if l >= r: return
m = l + r >> 1
merge_sort(q,l,m)
merge_sort(q,m+1,r)
k = 0
i = l
j = m + 1
while i<=m and j<=r:
if q[i] <= q[j]: # 稳定
tem[k] = q[i]
i += 1
else:
tem[k] = q[j]
j += 1
k += 1
while i<=m:
tem[k] = q[i]
i += 1
k += 1
while j<=r:
tem[k] = q[j]
j += 1
k += 1
q[l:r+1] = tem[:k]
n = int(input())
tem = [0 for i in range(n+10)]
lis = list(map(int,input().strip().split()))
merge_sort(lis,0,n-1)
for i in lis:
print(i,end=' ')
边栏推荐
- GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
- July Bootcamp (Day 31) - Status Compression
- ROS2 series of knowledge (4): understand the concept of [service]
- MYSQL-Batch insert data
- Google Earth Engine - Error resolution of Error: Image.clipToBoundsAndScale, argument 'input': Invalid type
- cmake入门学习笔记
- Chinese version of Pylint inspection rules
- leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]
- By Value or By Reference
- The principle of virtual inheritance
猜你喜欢
随机推荐
Web3.0: Building an NFT Market (1)
值传递还是引用传递(By Value or By Reference)
【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 1981期】
MYSQL关键字Explain解析
IDEA无法识别module(module右下角没有蓝色小方块)
Four ways the Metaverse is changing the way humans work
你需要知道的 TCP 四次挥手
机器学习初学者可以学哪些实战项目?
RTL8762DK RTC (5)
RTL8762DK Lighting/LED (3)
MYSQL二阶段提交
YOLO怎么入门?怎么实现自己的训练集?
Key Points Estimation and Point Instance
MYSQL逻辑架构
cmake入门学习笔记
MYSQL-Batch insert data
蓝图:杨辉三角排列
WAASAP WebClient UI页面标签的决定逻辑介绍
Soft Exam Senior System Architect Series: Basic Knowledge of Information Systems
Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni






