Problem 2651 --Sort I - Bubble Sort

2651: Sort I - Bubble Sort

"
Time Limit $1$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $624$ 正确数量 $532$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签 排序

Bubble Sort

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-1

2   for j = A.length-1 downto i+1

3      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.

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.

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.

5
5 3 2 4 1
1 2 3 4 5
8
1 ≤ N ≤ 100

推荐代码 查看2651 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$0 $ms] AOJ大管家 823301 2022-03-20 09:20:39
内存最少[$1120 $KB] 花开富贵 820229 2022-03-13 23:06:43
第一AC 卑微小赵 286177 2018-10-13 09:10:05
第一挑战 卑微小赵 286176 2018-10-13 09:07:21

赛题来源/所属竞赛 会津大学《挑战数据结构与算法》 挑战数据结构与算法

竞赛编号 竞赛名称 竞赛时间 访问比赛
1786 2023-2024-1学期《程序设计技能实训》排序和线性表【23计算机】 2023-12-18 00:00:00 请登录
1682 2021-2022-2学期<算法分析与设计> 第3周练习:递归和基础排序 2022-03-07 08:00:00 请登录
1608 2020-2021-2学期<算法分析与设计>第4周练习:排序算法 2021-03-15 08:00:00 请登录
1571 2020-2021-1学期《程序设计技能实训》基础排序【20计算机12345】 2020-12-22 09:00:00 请登录
1299 《 2019春季ACM/NOI高级算法集训班》训练一:排序 2019-03-02 14:00:00 请登录