Bubble Sort Aizu - ALDS1_2_A
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order.
编写一个气泡排序算法程序,按升序对序列A进行排序。
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.
注意,数组元素的索引基于0原点。
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
您的程序还应该打印伪代码第4行中定义的交换操作数。
Input
The first line of the input includes an integer N, the number of elements in the sequence.
输入的第一行包括一个整数n,即序列中的元素数。
In the second line, N elements of the sequence are given separated by spaces characters.
在第二行中,序列的n个元素由空格字符分隔。
Output
The output consists of 2 lines.
输出由2条线组成。
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
5
5 3 2 4 1
Sample Output 1
1 2 3 4 5
8
Sample Input 2
6
5 2 4 6 1 3
Sample Output 2
1 2 3 4 5 6
9
code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 ^ 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^^ 00. ^^0.^1 ^ 0 ^^110.^0. 0 ^ ^^^10.01^^ 010^ 1 1 ^^^1110.10001 10 0 ^ 1.1 ^^^11111100^ 10 . 01 ^^ ^^ ^^^1111^1.^ ^^^10 10^ 0^ ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*/
// Virtual_Judge —— Bubble Sort Aizu - ALDS1_2_A.cpp created by VB_KoKing on 2019,04,28,12.
/* Procedural objectives:
冒泡排序并统计交换次数Variables required by the program:1.全局变量ans计算交换次数2.局部变量n表示数组的大小3.存储元素的数组array[100]Procedural thinking:1.冒泡程序伪代码改成代码Functions required by the program:1.冒泡排序函数
*/
/* My dear Max said:
"I like you,
So the first bunch of sunshine I saw in the morning is you,
The first hurricane that passed through your ear is you,
The first star you see is also you.
The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!!
*/
#include <iostream>
#include <cstring>using namespace std;
int ans = 0, array[100];void BubbleSort(int A[], int n) {for (int i = 0; i < n - 1; i++)for (int j = n - 1; j > i; j--)if (A[j] < A[j - 1]) {swap(A[j], A[j - 1]);ans++;}
}int main() {int n;cin >> n;memset(array, 0, sizeof(array));for (int i = 0; i < n; i++)cin >> array[i];BubbleSort(array, n);for (int i = 0; i < n; i++) {cout << array[i];if (i != n - 1) cout << ' ';}cout << endl << ans << endl;return 0;
}
Bubble Sort Aizu - ALDS1_2_A相关推荐
- Stable Sort Aizu - ALDS1_2_C
Let's arrange a deck of cards. There are totally 36 cards of 4 suits(S, H, C, D) and 9 values (1, 2, ...
- 排序 时间倒序_经典排序算法之冒泡排序(Bubble Sort)
冒泡排序 ( Bubble Sort ) 冒泡排序,正如它的名字一样,未排序数组中的最大(小)值会依次往上浮.冒泡排序主要有两个基本步骤:相邻元素之间的比较 和 交换位置. 步骤分析: 令待排序序列为 ...
- Recursive Bubble Sort(递归冒泡排序)
程序来源:Recursive Bubble Sort 迭代冒泡排序算法: // Iterative Bubble Sort bubbleSort(arr[], n) {for (i = 0; i &l ...
- 冒泡排序的PHP实现 Bubble Sort
冒泡排序Bubble Sort的PHP实现.代码中函数说明: out_arr,用于将数组输出成一个字符串,以便查看 bubblesort,第一种实现方案,从后往前依次选出需要的值,这里是较大的 bub ...
- 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)
Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...
- python冒泡排序_5种python方法实现冒泡排序可视化:Bubble Sort Visualizer
1 说明: ===== 1.1 冒泡排序: 1.1.1 原理:比较两个相邻的元素,将值大的元素交换至右端. 1.1.2 来源:由来是因为越大的元素会经由交换慢慢"浮"到数列的顶端, ...
- 经典排序算法 - 冒泡排序Bubble sort
经典排序算法 - 冒泡排序Bubble sort 其原理是比较接近的数字22,按照从小到交换大或降序排列, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头開始进行两两比較交换,直到倒 ...
- 7.使用php描述冒泡排序,PHP 数据结构 算法描述 冒泡排序 bubble sort
PHP 数据结构 算法描述 冒泡排序 bubble sort 复制代码 代码如下: /** * 冒泡排序 bubble sort * * 原理:多次循环进行比较,每次比较时将最大数移动到最上面.每次循 ...
- 算法—1,简单说说最常见的冒泡排序(Bubble Sort)
一,引入. 冒泡排序可以说是学习算法知识的时候入门的一个算法了,可是自从我当了面试官之后,来面试的人却连冒泡写不出来,实在让我大跌眼镜,恰好最近一段时间想再深入学习一下算法,就单开一个分类,算法,来讲 ...
最新文章
- 重新绘制TabControl的Tabpage标签,添加图片及关闭按钮
- html代码范例_最佳HTML范例和HTML5范例
- Java讲课笔记07:计数循环与嵌套循环
- 输出IMG格式SAR图像——Img格式图像文件概述
- JS:三种常用的函数定义方式
- origin matlab调用,origin与matlab使用教程
- 磊科nw336 linux驱动程序,磊科NW336无线网卡驱动程序
- pla3d打印材料密度_FDM 3D打印机的常用耗材PLA的密度 创想三维
- 免费使用短信服务接口 ----用Java实现
- PreaparedStatement中execut的使用
- java 表单验证必填的_avalon2 非必填项的表单验证规则
- 微信抖音的服务器,抖音微信登陆未获得权限怎么办
- 第一次OllyDbg逆向记录(分析思路和注意点其他文章)
- #91;#12304;#26469;#20998;#26399;#23458;#26381;#30005;#35805;#12305;#93;
- 真正中文攻略之 SAKURA~雪月華~ 花鳥風月プレミアムエディション 汉化、攻略...
- 推特情感分析-基于spark
- padStart(),padEnd()方法
- Hey AI,请写一首披头士风格的歌给我
- 用计算机的笔来画来画画说课稿,电脑画画教案设计
- 详解centos6和centos7防火墙的关闭