当前位置:网站首页>787. Merge Sort
787. Merge Sort
2022-08-01 01:34: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=' ')
边栏推荐
猜你喜欢

IDEA无法识别module(module右下角没有蓝色小方块)

设备树的树形结构到底是怎样体现的?

Cmake introductory study notes

RTL8762DK 点灯/LED(三)

Data Middle Office Construction (VII): Data Asset Management
![leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]](/img/b9/7bd33bd981ace25e3bbfc7be9117ff.png)
leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]

【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】

北京突然宣布,元宇宙重大消息

Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)

MVCC总结
随机推荐
MYSQL-批量插入数据
Design the message queue storage MySQL form of message data
MYSQL two-phase commit
Soft Exam Senior System Architect Series: Basic Knowledge of Information Systems
Nmap Operation Manual - Full Version
MYSQL经典面试题
Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
Item 36: Specify std::launch::async if asynchronicity is essential.
MYSQL Classic Interview Questions
网关gateway跨域
leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]
SC7A20 (Silan Micro-Accelerometer) Example
元宇宙改变人类工作模式的四种方式
Rasa 3.x Study Series - Rasa - Issues 4918 Study Notes
cmake入门学习笔记
RTL8762DK WDG (six)
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
手写二叉查找树及测试
The IDEA can't find or unable to load The main class or Module "*" must not contain The source root "*" The root already belongs to The Module "*"
JVM面试题总结(持续更新中)