[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 ...
随机推荐
- C#动态表达式计算
C#动态表达式计算 应该有不少人开发过程中遇到过这样的需求,我们直接看图说话: 如上图所示,其中Entity为实体类,其中包括五个属性,该五个属性的值分别来自于数据库查询结果: 用户通过可视化界面进行 ...
- 【值得收藏】Mathematica数值计算工具的学习资料汇编【可免费下载】
Mathematica学习教程 Mathematica是一款科学计算软件,很好地结合了数值和符号计算引擎.图形系统.编程语言.文本系统.和与其他应用程序的高级连接.Mathematica与Matlab ...
- 简单的三层asp.net webForm使用Ninject实现Ioc
简单的三层asp.net webForm使用Ninject实现Ioc 在asp.net webform下使用Ninject的简单过程. 首先建立个项目,如下图,简单三层(PS:UI层要同时引用BLL. ...
- SpecFlow
SpecFlow http://blog.csdn.net/yujunwu2525/article/details/7839859 将业务需求与.NET代码结合起来 SpecFlow旨在弥合领域专家与 ...
- 了解mongoDB存储结构
mongoDB 深入浅出一 了解mongoDB存储结构 MongoDB 深入浅出 数据逻辑结构 1 mongoDB中的文档(document) 相当于 关系性数据库的一条一条的记录 2 colle ...
- 《剑指Offer》面试题-从头到尾打印链表
题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例.每一组测试案例包含多行,每行一个大于0的整数,代表一个链表的节点.第一行是链表第一个节点的值,依次类推 ...
- 2 MySQL安装
目录: 1. mysql安装简介2. 下载AppServ3. 安装AppServ4. 使用phpmyadmin连接MySQL5. 使用MySQL Command Line Client 连接MySQL ...
- UVA 10392 (13.07.28)
Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...
- C#与Arduino通过串口通信来控制LED灯的状态
一.引言 最近摆弄了一段时间的Arduino,发现Arduino做一些电子类项目.监控.机器人.电子玩具比较容易,并且Arduino与.NET程序集成也不难.接下来介绍一个简单的小程序,C#做的一个W ...
- 【hanoi】hanoi移动次数和移动步骤
#include<stdio.h> #include<stdlib.h> //移动次数 int hanoi(int num) { ; ) { ; } else { all = ...