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) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...
随机推荐
- PID控制算法的C语言实现十一 模糊算法简介
在PID控制算法的C语言实现九中,文章已经对模糊PID的实质做了一个简要说明.本来打算等到完成毕业设计,工作稳定了再着力完成剩下的部分.鉴于网友的要求和信任,抽出时间来,对模糊PID做一个较为详细的论 ...
- Codeforces Round #340 (Div. 2) E 莫队+前缀异或和
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- POJ2234:Matches Game(Nim博弈)
Matches Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12325 Accepted: 7184 题目链 ...
- JSP2 特性
JSP2 新特性 1.直接配置 JSP 属性 2.表达式语言 3.简化的自定义标签 API 4.Tag 文件语法 如果要使用 JSP2 语法,web.xml 文件必须使用 Servlet2.4 以上版 ...
- Covariance 协方差分析
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- libcurl在mingw下编译
通过命令提示符进入 curl-7.27.0 文件夹输入 mingw32-make mingw32 进行生成(这里我只需要普通的功能,于是没有加附加的选项)编译完成后,在 lib 文件夹中会有我们需要的 ...
- AndroidStudio环境搭建
简单记录一下AS的环境搭建过程,包括SVN的使用. 一.下载和安装JDK 地址:http://www.oracle.com/technetwork/java/javase/downloads/inde ...
- ictclas bug修复
大体上参考链接:http://blog.csdn.net/luojinping/article/details/8788743 最后注意下SegTag.java文件 public SegTag(int ...
- mongodb角色权限
角色说明:Read:允许用户读取指定数据库readWrite:允许用户读写指定数据库dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建.删除,查看统计或访问system.profileus ...
- sweetAlert2
SweetAlert2一个前端最好用的弹窗