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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Insertion Sort Aizu - ALDS1_1_A

發布時間:2024/5/6 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Insertion Sort Aizu - ALDS1_1_A 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Write a program of the Insertion Sort algorithm which sorts a sequence A in ascending order.
編寫插入排序算法的程序,按升序對序列A排序。
The algorithm should be based on the following pseudo code:
該算法應基于以下偽代碼:

for i = 1 to A.length-1
key = A[i]
/* insert A[i] into the sorted sequence A[0,…,j-1] */
j = i - 1
while j >= 0 and A[j] > key
A[j+1] = A[j]
j–
A[j+1] = key
Note that, indices for array elements are based on 0-origin.
注意,數組元素的索引基于0原點。

To illustrate the algorithms, your program should trace intermediate result for each step.
為了說明算法,您的程序應該跟蹤每個步驟的中間結果。

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 a single space.
在第二行中,序列的n個元素由一個空格分隔。

Output 輸出

The output consists of N lines.
輸出由n行組成。
Please output the intermediate sequence in a line for each step.
請為每個步驟一行輸出中間序列。
Elements of the sequence should be separated by single space.
序列的元素應該用單個空格分隔。

Constraints 約束條件

1 ≤ N ≤ 100

Sample Input 1
6
5 2 4 6 1 3
Sample Output 1
5 2 4 6 1 3
2 5 4 6 1 3
2 4 5 6 1 3
2 4 5 6 1 3
1 2 4 5 6 3
1 2 3 4 5 6
Sample Input 2
3
1 2 3
Sample Output 2
1 2 3
1 2 3
1 2 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 ^ .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 —— Insertion Sort Aizu - ALDS1_1_A.cpp created by VB_KoKing on 2019,04,28,08. /* Procedural objectives:Procedural thinking:Functions required by the program:Variables required by the program:*/ /* 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 n, A[100];void print() {for (int i = 0; i < n; i++) {cout << A[i];if (i != n - 1) cout << ' ';}cout << endl; }void insertionSort(int A[], int N) {for (int i = 1; i < N; i++) {//從第一個元素開始處理,到第n-1個元素結束int v = A[i], j = i - 1;while (j >= 0 && A[j] > v) {//當j>=0并且前一個元素大于當前元素的值時進入循環A[j + 1] = A[j];j--;}//前一個元素不大于當前元素的時退出循環A[j + 1] = v;print();} }int main() {cin >> n;memset(A, 0, sizeof(A));for (int i = 0; i < n; i++)cin >> A[i];print();insertionSort(A, n);return 0; }

總結

以上是生活随笔為你收集整理的Insertion Sort Aizu - ALDS1_1_A的全部內容,希望文章能夠幫你解決所遇到的問題。

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