博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aizu - ALDS1_2_A Bubble Sort 冒泡排序
阅读量:3903 次
发布时间:2019-05-23

本文共 1363 字,大约阅读时间需要 4 分钟。

Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

BubbleSort(A)1 for i = 0 to A.length-12     for j = A.length-1 downto i+13         if A[j] < A[j-1]4             swap A[j] and A[j-1]

Note that, indices for array elements are based on 0-origin.

Your program should also print the number of swap operations defined in line 4 of the pseudocode.

Input

The first line of the input includes an integer N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by spaces characters.

Output

The output consists of 2 lines.

In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

In the second line, please print the number of swap operations.

Constraints

1 ≤ N ≤ 100

Sample Input 1

55 3 2 4 1

Sample Output 1

1 2 3 4 58

 

Sample Input 2

65 2 4 6 1 3

Sample Output 2

1 2 3 4 5 69

代码如下:

#include 
#include
#include
#include
using namespace std;const int maxn=105;int n;int a[maxn];void BubbleSort (){ int sum=0; for (int i=0;i
a[j+1]) { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; sum++; } for (int i=0;i

 

转载地址:http://ppaen.baihongyu.com/

你可能感兴趣的文章
【转】信息奥赛一本通1185:单词排序(OJ题目描述有问题)
查看>>
webclient
查看>>
从百度MP3搜索结果中提取歌曲列表
查看>>
Python Set
查看>>
SWT 中实现最小化到托盘图标,并只能通过托盘的弹出菜单关闭程序
查看>>
Java Table Examples
查看>>
Java read file
查看>>
界面主线程,子线程更新主界面控件
查看>>
敲两遍引号键才出现
查看>>
latex subfigure
查看>>
latex /figure /label 序号 /ref /label 序号 不一致
查看>>
latex pageref 指明图片在某一页
查看>>
latex jpg转成eps
查看>>
Tripwire 配置 使用
查看>>
我问佛(仓央嘉措)
查看>>
win7 动态磁盘 无法安装fedora
查看>>
win7 动态硬盘
查看>>
Openoffice fedora/linux build
查看>>
openoffice development
查看>>
no office executable found! error still exist after adding /openoffice.org3/program to classpath
查看>>