当前位置:网站首页>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=' ')
边栏推荐
- Chain programming, packages, access
- Detailed explanation of TCP protocol
- Flink deploys and submits jobs
- RTL8762DK uses DebugAnalyzer (four)
- Chinese version of Pylint inspection rules
- OSD读取SAP CRM One Order应用日志的优化方式
- 【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
- Super like the keyboard made from zero, IT people love it
- Solve the problem that Excel opens very slowly after installing MySQL
- What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
猜你喜欢
VPGNet
Key Points Estimation and Point Instance
MYSQL Keyword Explain Analysis
STK8321 I2C (Shengjia-accelerometer) example
MYSQL master-slave replication
设备树——dtb格式到struct device node结构体的转换
RTL8762DK 点灯/LED(三)
Introduction to machine learning how to?
Academicians of the two academies speak bluntly: Don't be superstitious about academicians
JVM面试题总结(持续更新中)
随机推荐
Summary of JVM interview questions (continuously updated)
MYSQL logical architecture
leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
软考高级系统架构设计师系列之:系统开发基础知识
考研备考方案
Soft Exam Senior System Architect Series: Basic Knowledge of Information Systems
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 "*"
如何编辑epub电子书的目录
Design the message queue storage MySQL form of message data
MYSQL经典面试题
【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 1981期】
IDEA modifies the annotation font
值传递还是引用传递(By Value or By Reference)
GDB 源码分析系列文章五:动态库延迟断点实现机制
Data Middle Office Construction (VII): Data Asset Management
软考高级系统架构设计师系列之:信息系统基础知识
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
Blueprint: Yang Hui's Triangular Arrangement
Chain programming, packages, access
解决安装MySQL后,Excel打开很慢的问题