[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 ...
随机推荐
- AngularJS的依赖注入方式
在定义controller,module,service,and directive时有两种方式, 方式一: var myModule = angular.module('myApp', []); m ...
- .NET MVC4 实训记录之五(访问自定义资源文件)
.Net平台下工作好几年了,资源文件么,大多数使用的是.resx文件.它是个好东西,很容易上手,工作效率高,性能稳定.使用.resx文件,会在编译期动态生成已文件名命名的静态类,因此它的访问速度当然是 ...
- status状态栏实现字符串走动
<script type="text/javascript" language="javascript"> var i = 0; var str=& ...
- Ninject是一款.Net平台下的开源依赖注入框架
Ninject是一款.Net平台下的开源依赖注入框架.按照官方说法,它快如闪电.超级轻量,且充分利用了.Net的最新语法,使用Lambda表达式代替Xml文件完成类型绑定.Ninject结构精巧,功能 ...
- 由link()和symlink()谈到软链接与硬链接
任何一个文件可以有多个目录项指向其i节点.创建一个向现存文件连接的方法是使用l i n k函数. #include <unistd.h> int link(const char * e x ...
- c++类的构造函数与析构函数
为什么用构造函数与析构函数 构造函数: c++目标是让使用类对象就像使用标准类型一样,但是常规化的初始化句法不适用与类类型. ; //基本类型 struct thing { char *pn; int ...
- 利用MARQUEE实现正在处理效果
ASP.NET服务器端事件利用MARQUEE实现正在处理效果 前言:ASP.NET同仁们应该都遇到过当触发一个比较耗时的服务器端事件时,页面会处在一个等待的状态(即假死状态),用户体验非常不好,很 ...
- Linux网络编程(二)
Linux网络编程(二) 使用多进程实现服务器并发访问. 采用多进程的方式实现服务器的并发访问的经典范例. 程序实现功能: 1.客户端从标准输入读入一行文字,发送到服务器. 2.服务器接收到客户端发来 ...
- 【转】CSS中position属性( absolute | relative | static | fixed )详解
我们先来看看CSS3 Api中对position属性的相关定义: static:无特殊定位,对象遵循正常文档流.top,right,bottom,left等属性不会被应用. relative:对象遵循 ...
- socket网络编程快速上手(二)——细节问题(5)(完结篇)
6.Connect的使用方式 前面提到,connect发生EINTR错误时,是不能重新启动的.那怎么办呢,是关闭套接字还是直接退出进程呢?如果EINTR前,三次握手已经发起,我们当然希望链路就此已经建 ...