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 3
Hint
Template in C
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 —— 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1131:基因相关性
- 下一篇: 1132:石头剪子布