当前位置:网站首页>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=' ')
边栏推荐
- Basic implementation of vector
- WAASAP WebClient UI页面标签的决定逻辑介绍
- What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
- 带wiringPi库在unbutu 编译 并且在树莓派运行
- WebApi 打个Attribute 统一处理异常
- The kernel of the decompression process steps
- RTL8762DK UART (two)
- 软考高级系统架构设计师系列之:信息系统基础知识
- MYSQL transactions
- 链式编程、包、访问权限
猜你喜欢

Modern Enterprise Architecture Framework 1
![[Data analysis] Based on matlab GUI student achievement management system [including Matlab source code 1981]](/img/65/b84443b98c28d2728e9ae44b1294fb.jpg)
[Data analysis] Based on matlab GUI student achievement management system [including Matlab source code 1981]

Detailed explanation of TCP protocol

Game Security 03: A Simple Explanation of Buffer Overflow Attacks

MYSQL逻辑架构

Basic implementation of vector

MYSQL two-phase commit

IDEA debugging

Google Earth Engine - Error resolution of Error: Image.clipToBoundsAndScale, argument 'input': Invalid type

RTL8762DK uses DebugAnalyzer (four)
随机推荐
date command
Game Security 03: A Simple Explanation of Buffer Overflow Attacks
ROS2系列知识(4): 理解【服务】的概念
RTL8762DK UART(二)
ECCV2022 Workshop | 复杂环境中的多目标跟踪和分割
【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
How is the tree structure of the device tree reflected?
数据中台建设(七):数据资产管理
STK8321 I2C(昇佳-加速度传感器)示例
GDB 源码分析系列文章五:动态库延迟断点实现机制
sqlserver cannot connect remotely
Soft Exam Senior System Architect Series: Basic Knowledge of System Development
树莓派 的 arm 版的 gcc 安装 和环境变量的配置
如何编辑epub电子书的目录
MYSQL logical architecture
500 miles
设备树的树形结构到底是怎样体现的?
Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
Rasa 3.x Learning Series - Using Lookup Tables to Improve Entity Extraction
Completely closed Chrome updated and in the top right corner of the tip