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 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)的更多相关文章
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- Sort list by merge sort
使用归并排序对链表进行排序 O(nlgn) 的时间效率 /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join
nested loops join(嵌套循环) 驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...
- 归并排序(Merge Sort)
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- Summary: Merge Sort of Array && 求逆序对
常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...
- 基础排序算法之并归排序(Merge Sort)
并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...
随机推荐
- Unix处理目标文件的工具
- Visual Studio Code 如何编写运行 C、C++ 程序?
0. 前言 VS Code 是微软发布一款跨平台的源代码编辑器,其拥有强大的功能和丰富的扩展,使之能适合编写许多语言. 本文面向初学者(但不是纯小白),分享一点我配置C/C++的经验. 本文所有内容均 ...
- ECNA-A- Abstract Art
题目描述 Arty has been an abstract artist since childhood, and his works have taken on many forms. His l ...
- C11线程管理:异步操作
1.异步操作 C++11提供了异步操作相关的类,std::future.std::promise和std::package_task.std::future作为异步结果的传输通道,方便的获取线程函数的 ...
- 35、def func(a,b=[]) 这种写法有什么坑?
那我们先通过程序看看这个函数有什么坑吧! def func(a,b=[]): b.append(a) print(b) func(1) func(1) func(1) func(1) 看下结果 [1] ...
- oracle01--单表查询
1. 基本(基础)查询 1.1. 基本查询语法 基本查询是指最基本的select语句. [语法] [知识点]如何使用工具进行查询 在plsql developer中打开查询窗口(执行sql语句): 执 ...
- WordPress404页面自定义
不知道大家是怎么设计404页面,个性的404可以为网站增色不少,wordpress设置404是在主题里面的404.php页面上,当然比如你用Apache.nginx等服务器,你可以自己建一个单页,内容 ...
- css3旋转、过渡、动画属性
1.transform 该属性对元素进行旋转.缩放.移动和倾斜 translate元素从当前位置移动 rotate元素顺时针旋转 scale元素的尺寸增大或减小 skew元素翻转 2.transiti ...
- UIAutomation Diagram
- itext 生成pdf文件添加页眉页脚
原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...