当前位置:网站首页>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=' ')
边栏推荐
- Basic implementation of vector
- Simple vim configuration
- Chinese version of Pylint inspection rules
- 值传递还是引用传递(By Value or By Reference)
- MYSQL关键字Explain解析
- Super like the keyboard made from zero, IT people love it
- 现代企业架构框架1
- RTL8762DK UART(二)
- Solve the problem that when IDEA creates a new file by default, right-click, new, there is no XML file
- 【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
猜你喜欢
随机推荐
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
Detailed explanation of TCP protocol
Flink 部署和提交job
Web3.0:构建 NFT 市场(一)
Blueprint: Yang Hui's Triangular Arrangement
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
OSD读取SAP CRM One Order应用日志的优化方式
【历史上的今天】7 月 31 日:“缸中之脑”的提出者诞生;Wi-Fi 之父出生;USB 3.1 标准发布
What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
C字符串数组反转
YOLO怎么入门?怎么实现自己的训练集?
The kernel of the decompression process steps
VPGNet
500 miles
Exam preparation plan
Basic usage concepts of vim
Game Security 03: A Simple Explanation of Buffer Overflow Attacks
Item 36: Specify std::launch::async if asynchronicity is essential.
GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
leetcode:1648. 销售价值减少的颜色球【二分找边界】








