日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Bubble Sort Aizu - ALDS1_2_A

發布時間:2024/5/6 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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111^ ^^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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。