数据结构实验之排序二:交换排序 (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 ...
随机推荐
- 广度优先搜索(BFS)思路及算法分析
1.算法用途: 是一种图像搜索演算法.用于遍历图中的节点,有些类似于树的深度优先遍历.这里唯一的问题是,与树不同,图形可能包含循环,因此我们可能会再次来到同一节点. 2.主要思想: 主要借助一个队列. ...
- java之struts2之OGNL表达式
struts2推荐使用ognl表达式 ognl: object graph navigation language 对象导航图语言 如:school.teacher.address="北京& ...
- H5调起IOS原生商店支付
参考文档:http://www.html5plus.org/doc/zh_cn/payment.html 申请内购项目摘自 https://www.jianshu.com/p/1e79bfbe46e2 ...
- ERROR: Cannot uninstall 'chardet'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
pip 安装 docker库报错: ERROR: Cannot uninstall 'chardet'. It is a distutils installed project and thus we ...
- (五)react-native开发系列之Android原生交互
react-native可以做web与原生的交互,这是使用react-native开发项目的主要目的之一,也是主要优势,用rn而不用原生交互则毫无价值,这篇文章用来记录在项目中rn的原生交互使用过程. ...
- EasyUI DataGrid 根据ID选中行(转载)
转载来源: https://blog.csdn.net/chq00788/article/details/51505519 function selectRows(){ //获取需要选中的记录ID ...
- 不错的DSP和FPGA作者
https://blog.csdn.net/wordwarwordwar/article/details/90233903
- yum源仓库的三种搭建方式
yum源的三种搭建方式 一. 本地yum仓库的搭建 1.1.获取软件包资源 将iso镜像挂载在本地目录中,此次挂载目录为/var/www/html/repo/,此目录本身不存在,需要创建.软件宝资源 ...
- egg.js 完整实例2后台管理系统
项目地址 github.com/richard1015… 技术栈 Vue.js.iview.websocket.Amap 演示地址: 后台管理 schoolmgr.zhuzhida.vip 前台展示 ...
- 安装 docker-compose 配置 lnmp
1.安装docker-compose 确保已经安装了docker sudo curl -L "https://github.com/docker/compose/releases/downl ...