数据结构实验之排序二:交换排序 (SDUT 3399)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//int sum = 0;
int a[100004]; //题目是长整型,不过这里 int 也可以。。
int b[100005];
void qusort(int l, int r, int a[], int &sum) //这里如果不想传sum,就把sum定义成全局,如注释
{
int i = l, j = r;
int x = a[i];
if(i >= j) return ;
while(i < j)
{
while(i < j && a[j] >= x) j --;
if(a[j] != a[i]) sum ++;
a[i] = a[j];
while(i < j && a[i] <= x) i ++;
if(a[j] != a[i]) sum ++;
a[j] = a[i];
}
a[i] = x;
qusort(l,i-1,a,sum);
qusort(i+1,r,a,sum);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 0; i < n; i ++)
{
scanf("%d",&a[i]);
b[i] = a[i];
}
int num = 0; // 冒泡
int sum = 0; //快排
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < n - i - 1; j ++)
{
if(a[j] > a[j +1])
{
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
num ++;
}
}
}
qusort(0,n-1,b,sum);
printf("%d %d\n",num,sum);
}
return 0;
}
数据结构实验之排序二:交换排序 (SDUT 3399)的更多相关文章
- SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...
- SDUT OJ 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT-3399_数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 冒泡排序和快速排序都是基于"交 ...
- SDUT 3401 数据结构实验之排序四:寻找大富翁.!
数据结构实验之排序四:寻找大富翁 Time Limit: 150MS Memory Limit: 512KB Submit Statistic Problem Description 2015胡润全球 ...
- SDUT OJ 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之串二:字符串匹配
数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之排序四:寻找大富翁
数据结构实验之排序四:寻找大富翁 Time Limit: 200 ms Memory Limit: 512 KiB Submit Statistic Discuss Problem Descripti ...
- SDUT OJ 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem D ...
- SDUT OJ 数据结构实验之排序一:一趟快排
数据结构实验之排序一:一趟快排 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
随机推荐
- C# 数组排序带索引
想到了两种方法来实现,分别利用了List.Sort()和Dictionary.OrderBy()方法,代码如下: , , , , , , , , , }; //List.Sort() List< ...
- 可分离滤波器设计高斯滤波 CUDA程序优化, 实验记录
环境:RTX2060 ,1920X1080p ,循环10次, kernal_size=8 一 .测试前128个线程拷贝到dst数据的性能 ,只测试行卷积, block=(128+2r)X1 1. 使 ...
- 【洛谷 P4052】 [JSOI2007]文本生成器(AC自动机,DP)
题目链接 AC自动机上dp第一题嗷. 如果直接求可读文本的数量,显然要容斥,不好搞. 于是考虑求不可读文本的数量,再用\(26^m\)减去其即可. 建出AC自动机,如果一个节点为单词结尾或其fail链 ...
- 【洛谷 P3224】 [HNOI2012]永无乡(Splay,启发式合并)
题目链接 启发式合并就是暴力合并把小的合并到大的里,一个一个插进去. 并查集维护连通性,同时保证并查集的根就是所在Splay的根,这样能省去很多操作. #include <cstdio> ...
- eslint校验——开启服务后自动校验
"[javascriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode&q ...
- 阿里P7整理20个非常有用的Java程序片段,你知道吗?
1.字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric string int i = Integer.parseInt( ...
- MySQL连接查询流程源码
http://blog.itpub.net/29510932/viewspace-2129300/ 初始化: 点击(此处)折叠或打开 main |-mysqld |-my_init // 初始话线程变 ...
- IDEA设置项目文件自动Add到Svn/Git
1)配置自动Add 2)将未添加的文件添加到本地 3)取消已经添加的文件
- Availability-group DDL operations are permitted only when you are using the master database. Run the USE MASTER command, and retry your availability-group DDL command.
Question: SQL SERVER alwayson在向AG中添加DB最后一步在副本中将此DB添加入AG时报错: Availability-group DDL operations are p ...
- Jquery简单闭包
<html> <body> <script src="Js/Index.js"></script> <script type= ...