[SOJ]Easy sort (归并排序)
You know sorting is very important. And this easy problem is:
Given you an array with N non-negative integers which are smaller than 10,000,000, you have to sort this array. Sorting means that integer with smaller value presents first.
The first line of the input is a positive integer T. T is the number of the test cases followed.
The first line of each test case is a positive integer N (1<= N<= 1000) which represents the number of integers in the array. After that, N lines followed. The i-th line is the i-th integer in the array.
2
3
1
2
3
1
1
1
2
3
1
#include<cstdio>
#define MAX 1001
int a[MAX], aux[MAX]; //2路归并排序
void merge_sort(int lo, int hi)
{
if (lo < hi)
{
int mid = lo + (hi - lo)/2;
merge_sort(lo, mid); //递归
merge_sort(mid+1, hi); for (int i = lo; i <= hi; ++i) //赋初值
aux[i] = a[i]; int l = lo, r = mid+1;
for (int i = lo; i <= hi; i++)
{
if (l > mid) a[i] = aux[r++]; //第一路已经处理完
else if (r > hi) a[i] = aux[l++]; //第二路已经处理完
else if (aux[r] < aux[l]) a[i] = aux[r++]; //二路比较
else a[i] = aux[l++];
}
}
} int main()
{
int t, n; scanf("%d", &t); while(t--)
{
scanf("%d", &n); for (int i = 0; i < n; ++i)
scanf("%d", &a[i]); merge_sort(0, n-1); for (int i = 0; i < n; ++i)
printf("%d\n", a[i]);
} return 0;
}
[SOJ]Easy sort (归并排序)的更多相关文章
- nyoj322 sort 归并排序,树状数组
Sort 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You want to processe a sequence of n distinct integers b ...
- sicily 1154. Easy sort (tree sort& merge sort)
Description You know sorting is very important. And this easy problem is: Given you an array with N ...
- 1154. Easy sort
#include<iostream>#include<cmath>#include<iomanip>#include<algorithm>using n ...
- 《算法导论》读书笔记之排序算法—Merge Sort 归并排序算法
自从打ACM以来也算是用归并排序了好久,现在就写一篇博客来介绍一下这个算法吧 :) 图片来自维基百科,显示了完整的归并排序过程.例如数组{38, 27, 43, 3, 9, 82, 10}. 在算法导 ...
- 【Sort】Merge Sort归并排序
归并排序运行时间O(N log N),但是由于需要线性附加内存,所以很少用于主存排序. 算法核心在于以下三条语句,分治递归,分别对左半边和右半边的数组进行排序,然后把左右半边的数组一一进行比较放入数组 ...
- 选择排序、快速排序、归并排序、堆排序、快速排序实现及Sort()函数使用
1.问题来源 在刷题是遇到字符串相关问题中使用 strcmp()函数. 在函数比较过程中有使用 排序函数 Sort(beg,end,comp),其中comp这一项理解不是很彻底. #include & ...
- 【高级排序算法】2、归并排序法的实现-Merge Sort
简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...
- 【高级排序算法】1、归并排序法 - Merge Sort
归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...
- Sort Methods
heyheyhey ~~ It has been a long time since i come here again...whatever today i will summerize some ...
随机推荐
- 【IOS开发】基础
1.Objective-C 为 ANSI C 添加了下述语法和功能: 定义新的类 类和实例方法 方法调用(称为发消息) 属性声明(以及通过它们自动合成存取方法) 静态和动态类型化 块 (block), ...
- MVC AuthorizeAttribute 动态授权
开发中经常会遇到权限功能的设计,而在MVC 下我们便可以使用重写 AuthorizeAttribute 类来实现自定义的权限认证 首先我们的了解 AuthorizeAttribute 下面3个主要的方 ...
- [转]Installing Snow Leopard (Client) on VMware Fusion 6.0.3
Source: http://inficererk.wordpress.com/2014/05/29/installing-snow-leopard-client-on-vmware-fusion-6 ...
- Python基础-类的探讨(class)
Python基础-类的探讨(class) 我们下面的探讨基于Python3,我实际测试使用的是Python3.2,Python3与Python2在类函数的类型上做了改变 1,类定义语法 Python ...
- C# 号码归属地查询算法(根据Android来电归属地二进制文件查询修改)
前言 近期有个项目需要用到号码归属查询,归属地数据库可能比不上ip138,淘宝上也有卖的-,-! 文本提供一个279188条记录并压缩成562KB的归属地数据.我在互联网上搜索了相关文章,要不是数据库 ...
- 如何简单而优雅地升级Visual NMP中的PHP版本
需求:自己想测试下不同版本的PHP性能,就想升级下 Visual 这个集成环境中PHP的版本 网上: 升级PHP到5.6.11 1.下载新的nts版的PHP并解压缩到bin\PHP下,保留原文件夹的名 ...
- JSP和JavaBean
JSP JSP全称是Java Server Pages.JSP实际上就是Servlet. JSP原理:JSP会被Tomcat翻译成一个Servlet JSP语法 1)JSP的Java脚本表达式 作用: ...
- Mac下quick-cocos2d-x player 无法运行解决方案
今天打算在Mac开发我的游戏,因为游戏用的2.2.5版的player,没有安装版.在Mac上运行player时提示player文件已经损坏. 解决方法:在shell下执行如下指令即可 mv $QUIC ...
- php中empty和isset的区别
比如: $_POST[ 'test']= ' ';那么: isset($_POST[ 'test']) ===> true; empty($_POST[ 'test']) ===> tru ...
- 不再害羞,过程比结果更重要;分享一套 CodeSmit 代码生成模板。
住博客园 5 年了,以前也发过一些博文,但都在 一天后 / 几周后 / 几年后 将它删了:因为感觉代码写得不好:不清晰或侵入太大,哪怕只有一句侵入. 可是最近重写一套 CodeSmith 代码生成模板 ...