Bubble Sort

View as PDF

Submit solution

Points: 7
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type

FatalEagle is playing with N blocks, each with a distinct number T (0 \le T \le 256) on it. He is trying to learn how to sort the blocks in ascending order with bubble sort. He would like you to show him the steps to sort his blocks. Click here for a description of bubble sort.

Input Specification

The first line will have the integer N (0 \le N \le 20).

The next line will have N integers, each separated with a space.

Output Specification

Print the initial block sequence, then after each time bubble sort swaps two elements, print the current block sequence.

Sample Input 1

2
2 1

Sample Output 1

2 1
1 2

Sample Input 2

6
9 1 2 6 4 7

Sample Output 2

9 1 2 6 4 7
1 9 2 6 4 7
1 2 9 6 4 7
1 2 6 9 4 7
1 2 6 4 9 7
1 2 6 4 7 9
1 2 4 6 7 9

Comments


  • -21
    anasschoukri  commented on March 7, 2017, 11:54 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 7
    EG117  commented on Dec. 14, 2015, 3:11 p.m.

    If I am not mistaken this is neither Bubble nor selection, but some sort of hybrid. I am probably wrong but can someone explain?


  • -11
    omaewamoushindeiru  commented on Nov. 28, 2014, 11:09 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 5
      FatalEagle  commented on Nov. 28, 2014, 11:18 p.m.

      The bubble sort algorithm scans that array many times, each time swapping any adjacent elements that are out of order back into order. Once it swaps, it keeps searching through the rest of the array instead of going back to the beginning. Thus, the problem statement has the correct output.