Description

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.

Input

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.

Output

The output of each test case should consist of N lines. The i-th line is the i-th integer of the array after sorting. No redundant spaces are needed.

考试前写几个排序练练手……用这道题练了一下merge sort和tree sort

 #include<cstdio>
#define MAX 1001
int a[MAX], aux[MAX]; void merge_sort(int lo, int hi) {
if (lo < hi) {
int mid = lo + (hi - lo)/;
merge_sort(lo, mid);
merge_sort(mid+, hi); for (int i = lo; i <= hi; ++i)
aux[i] = a[i]; int l = lo, r = mid+;
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 argc, char *argv[]) {
int t, n; scanf("%d", &t); while(t--) {
scanf("%d", &n); for (int i = ; i < n; ++i)
scanf("%d", &a[i]); merge_sort(, n-); for (int i = ; i < n; ++i)
printf("%d\n", a[i]);
} return ;
}

tree sort直接拿以前笔试试卷上的二叉树题目做,所以带了模版。没有平衡,效率不太好(0.02s过)去掉插入操作里的检查重复元素之后就可以允许重复元素了。

#include <iostream>
#include <stack>
using namespace std; template<class Entry>
struct Binary_node {
Entry data;
Binary_node<Entry> *left;
Binary_node<Entry> *right;
bool flag;
Binary_node() { left = NULL; right = NULL; flag = false;}
Binary_node(const Entry &x) { data = x; left = NULL; right = NULL; flag = false;}
}; template<class Entry>
void print(const Entry &x) {
cout << x << '\n';
} template<class Entry>
class Binary_tree {
public:
Binary_tree() {
root = NULL;
} bool insert(const Entry &x) {
return insert(root, x);
} bool insert(Binary_node<Entry>* &r, const Entry &x) {
if (r == NULL) {
r = new Binary_node<Entry>(x); return true;
}
//if (x == r->data) return false;
if (x < r->data) return insert(r->left, x);
return insert(r->right, x);
} void inorder(void (*visit)(const Entry &)) {
std::stack<Binary_node<Entry> *> s;
Binary_node<Entry> *p = root;
while(!(p == NULL && s.empty())) {
while(p != NULL) {
s.push(p);
p = p->left;
} if (s.empty()) return;
p = s.top(); s.pop();
visit(p->data);
p = p->right;
}
} void del() {
delete(root);
} void del(Binary_node<Entry>* &r) {
if (r != NULL) {
if(r->left) del(r->left);
if(r->right) del(r->right);
delete r;
}
}
Binary_node<Entry> *root;
}; int main(int argc, char *argv[]) {
int t, n;
cin >> t;
int num; while(t--) {
cin >> n;
Binary_tree<int> bt;
for (int i = ; i < n; ++i) {
cin >> num;
bt.insert(num);
} bt.inorder(print<int>);
bt.del();
} return ;
}

sicily 1154. Easy sort (tree sort& merge sort)的更多相关文章

  1. 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)

    连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...

  2. Sort list by merge sort

    使用归并排序对链表进行排序 O(nlgn) 的时间效率 /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  3. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  4. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  5. SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join

    nested loops join(嵌套循环)   驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...

  6. 归并排序(Merge Sort)

    归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...

  7. 归并排序(merge sort)

    M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...

  8. Summary: Merge Sort of Array && 求逆序对

    常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...

  9. 基础排序算法之并归排序(Merge Sort)

    并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...

随机推荐

  1. [POI2005]Bank notes

    link 试题分析 我们发现此题是一个十分简单的多重背包.但是按照朴素写法会超时.所以要去考虑优化. 我们发现我们若$W=7$,可以拆成$1+2+4$,不用每次$1+1+1+1+1+1+1$,从$N$ ...

  2. CentOS 7网络故障

    By francis_hao    Nov 2,2017   在像往常一样打开了虚拟机后,打开xshell准备连接到centos,可是连不上,发现连接的网卡没有启动,使用systemctl启动netw ...

  3. wireshark 根据域名筛选

    应该去掉引号

  4. java中Mysql开发

    [IntelliJ IDEA 12使用]导入外部包 http://www.cnblogs.com/haochuang/p/3491959.html JDBC导入包即可 http://blog.163. ...

  5. stout代码分析之零

    最近在使用mesos做高可用设计,在编译的过程中注意到mesos依赖stout,一个仅仅含有头文件的c++基础库.stout代码简洁,设计优雅,值得一读. stout从内容上可细分为以下几块: Pri ...

  6. jquery ajax thinkphp异步局部刷新完整流程

    环境:ThinkPHP3.2.3,jQuery3.2   前言: 在一般的网站中,都需要用到jquery或者其他框架(比如angular)来处理前后端数据交互,thinkphp在后台也内置了一些函数用 ...

  7. [DeeplearningAI笔记]序列模型2.8 GloVe词向量

    5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.8 GloVe word vectors GloVe词向量 Pennington J, Socher R, Mannin ...

  8. 2017北京国庆刷题Day1 morning

    期望得分:100+100+100=300 实际得分:100+100+70=270 T1位运算1(bit) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK ...

  9. C11线程管理:原子变量&单调函数

    1.原子变量 C++11提供了原子类型std::atomic<T>,可以使用任意类型作为模板参数,使用原子变量就不需要使用互斥量来保护该变量,用起来更加简洁. 举个例子,如果要做一个计数器 ...

  10. unZip/Zip的安装

    1.apt-get安装: apt-get install zip 2.yum安装: yum install -y unzip zip