当前位置:网站首页>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=' ')
边栏推荐
- 内核的解压缩过程详解
- 从设备树(dtb格式数据)中解析出bootargs
- SC7A20(士兰微-加速度传感器)示例
- MYSQL two-phase commit
- 【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
- [cellular automata] based on matlab interface aggregation cellular automata simulation [including Matlab source code 2004]
- Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
- leetcode:1648. 销售价值减少的颜色球【二分找边界】
- Modify Postman installation path
- OSF一分钟了解敏捷开发模式
猜你喜欢
how to edit the table of contents of an epub ebook
【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO
RTL8762DK WDG(六)
MVCC总结
MYSQL-批量插入数据
数据中台建设(七):数据资产管理
IDEA无法识别module(module右下角没有蓝色小方块)
MYSQL关键字Explain解析
【密码学/密码分析】基于TMTO的密码分析方法
ECCV2022 Workshop | Multi-Object Tracking and Segmentation in Complex Environments
随机推荐
Nmap Operation Manual - Full Version
Luogu P3373: Segment tree
MYSQL transactions
【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
RTL8762DK UART (two)
Super like the keyboard made from zero, IT people love it
Rasa 3.x Learning Series - Using Lookup Tables to Improve Entity Extraction
IDEA调试
从零造键盘的键盘超级喜欢,IT人最爱
500 miles
手写二叉查找树及测试
Euler system (euleros): upgrade Mysql
What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
机器学习初学者可以学哪些实战项目?
RTL8762DK 点灯/LED(三)
Solve the problem that when IDEA creates a new file by default, right-click, new, there is no XML file
MYSQL主从复制
软考高级系统架构设计师系列之:信息系统基础知识
Inheritance Considerations
MYSQL logical architecture