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. php 阿拉伯数字转中文

    function numToWord($num){$chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');$chiUni = ...

  2. c++常量详解

    概念 常量是存放固定且不可变值的,一旦确定初始值则在程序其它地方不可改变, 所以const对象必须初始化.常量一般使用const关键字来修饰. const 对象可以大致分为三类: 1. const i ...

  3. 图像处理之均值滤波介绍及C算法实现

    1 均值滤波介绍 滤波是滤波是将信号中特定波段频率滤除的操作,是从含有干扰的接收信号中提取有用信号的一种技术. 均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板,该模板包括了其周围的临 ...

  4. springboot集成Guava缓存

    很久没有写博客了,这段时间一直忙于看论文,写论文,简直头大,感觉还是做项目比较舒服,呵呵,闲话不多说,今天学习了下Guava缓存,这跟Redis类似的,但是适用的场景不一样,学习下吧.今天我们主要是s ...

  5. libevent学习笔记(参考libevent深度剖析)

    最近自学libevent事件驱动库,参考的资料为libevent2.2版本以及张亮提供的<Libevent源码深度剖析>, 参考资料: http://blog.csdn.net/spark ...

  6. phpmyadmin 登录时间修改

    登录后1440秒未活动后总是自动退出,一天还要登录多次,终于有时间来解决这个问题了,感觉是session超时,结果在网上search了下,找到解决办法啦,哈哈哈,在此做个笔记:phpmyadmin在使 ...

  7. 利用Tensorflow读取二进制CIFAR-10数据集

    使用Tensorflow读取CIFAR-10二进制数据集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Tensorflow官方文档 tf.transpose函数解析 tf.sli ...

  8. jni 找不到本地方法的实现

    使用JNI开发,需要在java端声明本地方法,并在jni层实现本地方法. 有时运行项目时会先抛出异常:No implementation found for native xxx 然后直接挂掉:jav ...

  9. [LeetCode] 30. Substring with Concatenation of All Words ☆☆☆

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  10. HDU 1402 FFT 大数乘法

    $A * B$ FFT模板题,找到了一个看起来很清爽的模板 /** @Date : 2017-09-19 22:12:08 * @FileName: HDU 1402 FFT 大整数乘法.cpp * ...