SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序
Problem Description
冒泡排序和快速排序都是基于"交换"进行的排序方法,你的任务是对题目给定的N个(长整型范围内的)整数从小到大排序,输出用冒泡和快排对这N个数排序分别需要进行的数据交换次数。
Input
连续多组输入数据,每组数据第一行给出正整数N(N ≤ 10^5),随后给出N个整数,数字间以空格分隔。
Output
输出数据占一行,代表冒泡排序和快速排序进行排序分别需要的交换次数,数字间以1个空格分隔,行末不得有多余空格。
Example Input
8
49 38 65 97 76 13 27 49
Example Output
15 9
Hint
注意:数据相等时不做交换
DQE:
冒泡和快排,注意快排是怎么统计次数的,冒泡时下标从1和0开始时的循环条件的区别~
#include <iostream>
#include <cstdio>
using namespace std; int buble(int *f,int n)
{
int i,j,count=;;
for(i=;i<=n-;i++)
for(j=;j<=n-i;j++)
if(f[j]>f[j+])
{
count++;
int t=f[j];f[j]=f[j+];f[j+]=t;
}
return count;
} int qs(int *f,int l,int r)
{
int count=;
if(l>=r)
return ;
int i=l,j=r;
int k=f[i];
while(i<j)
{
while(i<j&&f[j]>=k)
j--;
f[i]=f[j];
if(i!=j)
count++;
while(i<j&&f[i]<=k)
i++;
f[j]=f[i];
if(i!=j)
count++;
}
f[i]=k;
return count+qs(f,l,i-)+qs(f,i+,r);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int f1[]={},f2[]={},i;
for(i=;i<=n;i++)
{
scanf("%d",f1+i);
*(f2+i)=*(f1+i);
}
printf("%d %d\n",buble(f1,n),qs(f2,,n));
}
return ;
} /***************************************************
User name: ***
Result: Accepted
Take time: 0ms
Take Memory: 928KB
Submit time: 2016-12-03 13:56:27
****************************************************/
SDUT 3399 数据结构实验之排序二:交换排序的更多相关文章
- 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 ...
- SDUT OJ 数据结构实验之排序八:快速排序
数据结构实验之排序八:快速排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 给定N ...
随机推荐
- Java操作Redis(代码演示)
redis-demo演示 一.创建一个maven工程 1.在pom.xml中引入相关redis的相关依赖 <project xmlns="http://maven.apache.org ...
- Linux命令学习(22):ss命令
版权声明 更新:2017-05-20 博主:LuckyAlan 联系:liuwenvip163@163.com 声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的ss命令 ...
- ACM学习历程—2016"百度之星" - 资格赛(Astar Round1)
http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 A题: 给定字符串,求任意区间的Hash值. 根据题目给定的Hash方式,属 ...
- 门禁 IC卡 ID 卡 RFID 手环 NFC 银行卡 手机模拟门禁
门禁 IC卡 ID 卡 RFID 手环 NFC 银行卡 手机模拟门禁 原因 最近给公司换了一个门禁. 旧的门禁按键面板已经破了. 不支持我的手环. 按了密码后竟然要按 #. 相关信息 查了资料记录一下 ...
- LOJ 10189 仓库建设 ——斜率优化dp
题目:https://loj.ac/problem/10189 #include<iostream> #include<cstdio> #include<cstring& ...
- 分区工具parted的详解及常用分区使用方法
一. parted的用途及说明 概括使用说明: parted用于对磁盘(或RAID磁盘)进行分区及管理,与fdisk分区工具相比,支持2TB以上的磁盘分区,并且允许调整分区的大小. ...
- C# 加密和解密文件
相关示例代码如下: using System; using System.IO; using System.Security; using System.Security.Cryptography; ...
- Vue项目开启步骤
## Build Setup # install cnpm //安装淘宝CNPM镜像npm install -g cnpm --registry=http://registry.npm.taobao. ...
- 往jdk/bin目录中增加tcnative-1.dll文件以后报错 Can't load AMD 64-bit .dll on a IA 32-bit platform
开始时,运行Tomcat控制台报错: The APR based Apache Tomcat Native library which allows optimal performance in pr ...
- expected declaration specifiers or '...' before string constant
/work/platform_bus_dev_drv/led_dev.c:52: error: expected declaration specifiers or '...' before stri ...