-->
当前位置:首页 > 题库

PROGRAMMING:The process of bubble sorting

Luz5年前 (2021-05-10)题库549
This problem requires the use of bubble sort, the given $$n $$integers from small to large sort output, and output sorting process in each step of the intermediate results.
The algorithm steps of bubble sort are described as follows:
Step $$1: in the unordered number of $$n $$($$a [0] $~ $$a [n-1] $), from $$a [0] $, compare two adjacent numbers in turn. If the adjacent elements do not meet the order requirements, exchange them. After this operation, the largest element in the array "bubbles" to $$a [n-1] $;
Step $$2: in the remaining unordered number of $$n-1 $$($$a [0] $~ $$a [n-2] $), from $$a [0] $, compare the two adjacent numbers in turn. If the adjacent elements do not meet the order requirements, exchange them. After this operation, the largest element in $$a [0] $~ $$a [n-2] $$bubbles to $$a [n-2] $;
……
Step $$I $: in the remaining unordered number of $$n-k $$($$a [0] ~ a [n-i] $), from $$a [0] $, compare the two adjacent numbers in turn, and if the adjacent elements do not meet the order requirements, exchange them. After this operation, the largest element in $$a [0] $~ $$a [n-i] $$bubbles to $$a [n-i] $;
……
Step $$n-1 $$: in the remaining unordered number of $$2 $$($$a [0] $~ $$a [1] $), compare the two numbers and exchange them if they do not meet the order requirements. After this operation, the largest element in $$a [0] $~ $$a [1] $$bubbles to $$a [1] $.
###Input format:
Enter the first line to give a positive integer of no more than 10 $$n $. The second line gives $$n $$integers separated by spaces.
###Output format:
In each row, output the intermediate result of the corresponding step in the sorting process, that is, the value of $$a [0] $~ $$a [n-1] $$after each step. There is a space between adjacent numbers, and there must be no extra space at the end of the row.
###Input example:
```in
five
8 7 6 0 1
```
###Output example:
```out
7 6 0 1 8
6 0 1 7 8
0 1 6 7 8
0 1 6 7 8
```







answer:If there is no answer, please comment