## Bubble Sort

View as PDF

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

Authors:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

FatalEagle is playing with blocks with each with a distinct number on them. 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 .

The next line have 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 2

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

• commented on Feb. 20, 2019, 9:23 p.m. edit 6

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 4 6 9 7 1 2 4 6 7 9

How it should be

• commented on March 7, 2017, 6:54 a.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on March 7, 2017, 10:16 a.m.
• commented on March 7, 2017, 1:31 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on March 7, 2017, 2:15 p.m.

Yes, because your code is incorrect and invokes undefined behaviour due to out-of-bounds array indexing. That's why you're getting a different number of WAs each run, because your code's behaviour is undefined.

• commented on March 7, 2017, 6:51 a.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Dec. 14, 2015, 10:11 a.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?

• commented on Dec. 4, 2014, 9:01 p.m.

Are you sure that there are no errors with the sort?

• commented on Dec. 4, 2014, 9:13 p.m.

As is the standard answer to this question, look at how many people got AC, and then decide whether it's more likely that they're all wrong or you are wrong. (You may have misunderstood the problem.)

• commented on Nov. 28, 2014, 6:09 p.m.
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 4 6 9 7
1 2 4 6 7 9
• commented on Nov. 28, 2014, 6: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.