Cody

# Problem 1401. Implement a bubble sort technique and output the number of swaps required

Solution 1193985

Submitted on 21 May 2017 by Noriko Hounoki
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
x = [5 1 4 2 8]; y_correct = 4; assert(isequal(bubblesort(x),y_correct))

num = 5 t = 4 x = 5 1 2 2 8 x = 5 1 2 4 8 c = 1 t = 5 x = 1 1 2 4 8 x = 1 5 2 4 8 c = 2 t = 5 x = 1 2 2 4 8 x = 1 2 5 4 8 c = 3 t = 5 x = 1 2 4 4 8 x = 1 2 4 5 8 c = 4

2   Pass
x = [7 9 17 4 19 3 17 13 20 18]; y_correct = 14; assert(isequal(bubblesort(x),y_correct))

num = 10 t = 20 x = 7 9 17 4 19 3 17 13 18 18 x = 7 9 17 4 19 3 17 13 18 20 c = 1 t = 17 x = 7 9 17 4 19 3 13 13 18 20 x = 7 9 17 4 19 3 13 17 18 20 c = 2 t = 19 x = 7 9 17 4 3 3 13 17 18 20 x = 7 9 17 4 3 19 13 17 18 20 c = 3 t = 4 x = 7 9 17 3 3 19 13 17 18 20 x = 7 9 17 3 4 19 13 17 18 20 c = 4 t = 17 x = 7 9 3 3 4 19 13 17 18 20 x = 7 9 3 17 4 19 13 17 18 20 c = 5 t = 9 x = 7 3 3 17 4 19 13 17 18 20 x = 7 3 9 17 4 19 13 17 18 20 c = 6 t = 7 x = 3 3 9 17 4 19 13 17 18 20 x = 3 7 9 17 4 19 13 17 18 20 c = 7 t = 19 x = 3 7 9 17 4 13 13 17 18 20 x = 3 7 9 17 4 13 19 17 18 20 c = 8 t = 17 x = 3 7 9 4 4 13 19 17 18 20 x = 3 7 9 4 17 13 19 17 18 20 c = 9 t = 9 x = 3 7 4 4 17 13 19 17 18 20 x = 3 7 4 9 17 13 19 17 18 20 c = 10 t = 7 x = 3 4 4 9 17 13 19 17 18 20 x = 3 4 7 9 17 13 19 17 18 20 c = 11 t = 19 x = 3 4 7 9 17 13 17 17 18 20 x = 3 4 7 9 17 13 17 19 18 20 c = 12 t = 17 x = 3 4 7 9 13 13 17 19 18 20 x = 3 4 7 9 13 17 17 19 18 20 c = 13 t = 19 x = 3 4 7 9 13 17 17 18 18 20 x = 3 4 7 9 13 17 17 18 19 20 c = 14

3   Pass
x = [73 72 27 56 16 12 21 12 19 1 9 52 44]; y_correct = 54; assert(isequal(bubblesort(x),y_correct))

num = 13 t = 52 x = 73 72 27 56 16 12 21 12 19 1 9 44 44 x = 73 72 27 56 16 12 21 12 19 1 9 44 52 c = 1 t = 19 x = 73 72 27 56 16 12 21 12 1 1 9 44 52 x = 73 72 27 56 16 12 21 12 1 19 9 44 52 c = 2 t = 12 x = 73 72 27 56 16 12 21 1 1 19 9 44 52 x = 73 72 27 56 16 12 21 1 12 19 9 44 52 c = 3 t = 21 x = 73 72 27 56 16 12 1 1 12 19 9 44 52 x = 73 72 27 56 16 12 1 21 12 19 9 44 52 c = 4 t = 12 x = 73 72 27 56 16 1 1 21 12 19 9 44 52 x = 73 72 27 56 16 1 12 21 12 19 9 44 52 c = 5 t = 16 x = 73 72 27 56 1 1 12 21 12 19 9 44 52 x = 73 72 27 56 1 16 12 21 12 19 9 44 52 c = 6 t = 56 x = 73 72 27 1 1 16 12 21 12 19 9 44 52 x = 73 72 27 1 56 16 12 21 12 19 9 44 52 c = 7 t = 27 x = 73 72 1 1 56 16 12 21 12 19 9 44 52 x = 73 72 1 27 56 16 12 21 12 19 9 44 52 c = 8 t = 72 x = 73 1 1 27 56 16 12 21 12 19 9 44 52 x = 73 1 72 27 56 16 12 21 12 19 9 44 52 c = 9 t = 73 x = 1 1 72 27 56 16 12 21 12 19 9 44 52 x = 1 73 72 27 56 16 12 21 12 19 9 44 52 c = 10 t = 19 x = 1 73 72 27 56 16 12 21 12 9 9 44 52 x = 1 73 72 27 56 16 12 21 12 9 19 44 52 c = 11 t = 12 x = 1 73 72 27 56 16 12 21 9 9 19 44 52 x = 1 73 72 27 56 16 12 21 9 12 19 44 52 c = 12 t = 21 x = 1 73 72 27 56 16 12 9 9 12 19 44 52 x = 1 73 72 27 56 16 12 9 21 12 19 44 52 c = 13 t = 12 x = 1 73 72 27 56 16 9 9 21 12 19 44 52 x = 1 73 72 27 56 16 9 12 21 12 19 44 52 c = 14 t = 16 x = 1 73 72 27 56 9 9 12 21 12 19 44 52 x = 1 73 72 27 56 9 16 12 21 12 19 44 52 c = 15 t = 56 x = 1 73 72 27 9 9 16 12 21 12 19 44 52 x = 1 73 72 27 9 56 16 12 21 12 19 44 52 c = 16 t = 27 x = 1 73 72 9 9 56 16 12 21 12 19 44 52 x = 1 73 72 9 27 56 16 12 21 12 19 44 52 c = 17 t = 72 x = 1 73 9 9 27 56 16 12 21 12 19 44 52 x = 1 73 9 72 27 56 16 12 21 12 19 44 52 c = 18 t = 73 x = 1 9 9 72 27 56 16 12 21 12 19 44 52 x = 1 9 73 72 27 56 16 12 21 12 19 44 52 c = 19 t = 21 x = 1 9 73 72 27 56 16 12 12 12 19 44 52 x = 1 9 73 72 27 56 16 12 12 21 19 44 52 c = 20 t = 16 x = 1 9 73 72 27 56 12 12 12 21 19 44 52 x = 1 9 73 72 27 56 12 16 12 21 19 44 52 c = 21 t = 56 x = 1 9 73 72 27 12 12 16 12 21 19 44 52 x = 1 9 73 72 27 12 56 16 12 21 19 44 52 c = 22 t = 27 x = 1 9 73 72 12 12 56 16 12 21 19 44 52 x = 1 9 73 72 12 27 56 16 12 21 19 44 52 c = 23 t = 72 x = 1 9 73 12 12 27 56 16 12 21 19 44 52 x = 1 9 73 12 72 27 56 16 12 21 19 44 52 c = 24 t = 73 x = 1 9 12 12 72 27 56 16 12 21 19 44 52 x = 1 9 12 73 72 27 56 16 12 21 19 44 52 c = 25 t = 21 x = 1 9 12 73 72 27 56 16 12 19 19 44 52 x = 1 9 12 73 72 27 56 16 12 19 21 44 52 c = 26 t = 16 x = 1 9 12 73 72 27 56 12 12 19 21 44 52 x = 1 9 12 73 72 27 56 12 16 19 21 44 52 c = 27 t = 56 x = 1 9 12 73 72 27 12 12 16 19 21 44 52 x = 1 9 12 73 72 27 12 56 16 19 21 44 52 c = 28 t = 27 x = 1 9 12 73 72 12 12 56 16 19 21 44 52 x = 1 9 12 73 72 12 27 56 16 19 21 44 52 c = 29 t = 72 x = 1 9 12 73 12 12 27 56 16 19 21 44 52 x = 1 9 12 73 12 72 27 56 16 19 21 44 52 c = 30 t = 73 x = 1 9 12 12 12 72 27 56 16 19 21 44 52 x = 1 9 12 12 73 72 27 56 16 19 21 44 52 c = 31 t = 56 x = 1 9 12 12 73 72 27 16 16 19 21 44 52 x = 1 9 12 12 73 72 27 16 56 19 21 44 52 c = 32 t = 27 x = 1 9 12 12 73 72 16 16 56 19 21 44 52 x = 1 9 12 12 73 72 16 27 56 19 21 44 52 c = 33 t = 72 x = 1 9 12 12 73 16 16 27 56 19 21 44 52 x = 1 9 12 12 73 16 72 27 56 19 21 44 52 c = 34 t = 73 x = 1 9 12 12 16 16 72 27 56 19 21 44 52 x = 1 9 12 12 16 73 72 27 56 19 21 44 52 c = 35 t = 56 x = 1 9 12 12 16 73 72 27 19 19 21 44 52 x = 1 9 12 12 16 73 72 27 19 56 21 44 52 c = 36 t = 27 x = 1 9 12 12 16 73 72 19 19 56 21 44 52 x = 1 9 12 12 16 73 72 19 27 56 21 44 52 c = 37 t = 72 x = 1 9 12 12 16 73 19 19 27 56 21 44 52 x = 1 9 12 12 16 73 19 72 27 56 21 44 52 c = 38 t = 73 x = 1 9 12 12 16 19 19 72 27 56 21 44 52 x = 1 9 12 12 16 19 73 72 27 56 21 44 52 c = 39 t = 56 x = 1 9 12 12 16 19 73 72 27 21 21 44 52 x = 1 9 12 12 16 19 73 72 27 21 56 44 52 c = 40 t = 27 x = 1 9 12 12 16 19 73 72 21 21 56 44 52 x = 1 9 12 12 16 19 73 72 21 27 56 44 52 c = 41 t = 72 x = 1 9 12 12 16 19 73 21 21 27 56 44 52 x = 1 9 12 12 16 19 73 21 72 27 56 44 52 c = 42 t = 73 x = 1 9 12 12 16 19 21 21 72 27 56 44 52 x = 1 9 12 12 16 19 21 73 72 27 56 44 52 c = 43 t = 56 x = 1 9 12 12 16 19 21 73 72 27 44 44 52 x = 1 9 12 12 16 19 21 73 72 27 44 56 52 c = 44 t = 72 x = 1 9 12 12 16 19 21 73 27 27 44 56 52 x = 1 9 12 12 16 19 21 73 27 72 44 56 52 c = 45 t = 73 x = 1 9 12 12 16 19 21 27 27 72 44 56 52 x = 1 9 12 12 16 19 21 27 73 72 44 56 52 c = 46 t = 56 x = 1 9 12 12 16 19 21 27 73 72 44 52 52 x = 1 9 12 12 16 19 21 27 73 72 44 52 56 c = 47 t = 72 x = 1 9 12 12 16 19 21 27 73 44 44 52 56 x = 1 9 12 12 16 19 21 27 73 44 72 52 56 c = 48 t = 73 x = 1 9 12 12 16 19 21 27 44 44 72 52 56 x = 1 9 12 12 16 19 21 27 44 73 72 52 56 c = 49 t = 72 x = 1 9 12 12 16 19 21 27 44 73 52 52 56 x = 1 9 12 12 16 19 21 27 44 73 52 72 56 ...

4   Pass
x = [4 15 2 5 1 5 8 6 12 13 22 7]; y_correct = 18; assert(isequal(bubblesort(x),y_correct))

num = 12 t = 22 x = 4 15 2 5 1 5 8 6 12 13 7 7 x = 4 15 2 5 1 5 8 6 12 13 7 22 c = 1 t = 13 x = 4 15 2 5 1 5 8 6 12 7 7 22 x = 4 15 2 5 1 5 8 6 12 7 13 22 c = 2 t = 12 x = 4 15 2 5 1 5 8 6 7 7 13 22 x = 4 15 2 5 1 5 8 6 7 12 13 22 c = 3 t = 8 x = 4 15 2 5 1 5 6 6 7 12 13 22 x = 4 15 2 5 1 5 6 8 7 12 13 22 c = 4 t = 5 x = 4 15 2 1 1 5 6 8 7 12 13 22 x = 4 15 2 1 5 5 6 8 7 12 13 22 c = 5 t = 2 x = 4 15 1 1 5 5 6 8 7 12 13 22 x = 4 15 1 2 5 5 6 8 7 12 13 22 c = 6 t = 15 x = 4 1 1 2 5 5 6 8 7 12 13 22 x = 4 1 15 2 5 5 6 8 7 12 13 22 c = 7 t = 4 x = 1 1 15 2 5 5 6 8 7 12 13 22 x = 1 4 15 2 5 5 6 8 7 12 13 22 c = 8 t = 8 x = 1 4 15 2 5 5 6 7 7 12 13 22 x = 1 4 15 2 5 5 6 7 8 12 13 22 c = 9 t = 15 x = 1 4 2 2 5 5 6 7 8 12 13 22 x = 1 4 2 15 5 5 6 7 8 12 13 22 c = 10 t = 4 x = 1 2 2 15 5 5 6 7 8 12 13 22 x = 1 2 4 15 5 5 6 7 8 12 13 22 c = 11 t = 15 x = 1 2 4 5 5 5 6 7 8 12 13 22 x = 1 2 4 5 15 5 6 7 8 12 13 22 c = 12 t = 15 x = 1 2 4 5 5 5 6 7 8 12 13 22 x = 1 2 4 5 5 15 6 7 8 12 13 22 c = 13 t = 15 x = 1 2 4 5 5 6 6 7 8 12 13 22 x = 1 2 4 5 5 6 15 7 8 12 13 22 c = 14 t = 15 x = 1 2 4 5 5 6 7 7 8 12 13 22 x = 1 2 4 5 5 6 7 15 8 12 13 22 c = 15 t = 15 x = 1 2 4 5 5 6 7 8 8 12 13 22 x = 1 2 4 5 5 6 7 8 15 12 13 22 c = 16 t = 15 x = 1 2 4 5 5 6 7 8 12 12 13 22 x = 1 2 4 5 5 6 7 8 12 15 13 22 c = 17 t = 15 x = 1 2 4 5 5 6 7 8 12 13 13 22 x = 1 2 4 5 5 6 7 8 12 13 15 22 c = 18

5   Pass
x = [48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 9]; y_correct = 180; assert(isequal(bubblesort(x),y_correct))

num = 30 t = 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 9 Column 30 9 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 9 Column 30 91 c = 1 t = 85 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 9 9 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 9 85 Column 30 91 c = 2 t = 71 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 9 9 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 9 71 85 Column 30 91 c = 3 t = 76 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 9 9 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 9 76 71 85 Column 30 91 c = 4 t = 37 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 9 9 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 9 37 76 71 85 Column 30 91 c = 5 t = 12 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 9 9 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 9 12 37 76 71 85 Column 30 91 c = 6 t = 99 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 9 9 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 9 99 12 37 76 71 85 Column 30 91 c = 7 t = 18 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 9 9 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 9 18 99 12 37 76 71 85 Column 30 91 c = 8 t = 67 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 9 9 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 9 67 18 99 12 37 76 71 85 Column 30 91 c = 9 t = 61 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 9 9 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 32 4 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 10 t = 32 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 4 4 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 52 4 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 11 t = 52 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 4 4 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 29 4 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 12 t = 29 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 4 4 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 91 4 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 13 t = 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 4 4 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 53 4 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 14 t = 53 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 4 4 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 27 4 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 15 t = 27 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 4 4 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 6 4 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 16 t = 6 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 4 4 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 57 2 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 17 t = 57 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 2 2 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 59 2 57 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 18 t = 59 x = Columns 1 through 29 48 15 16 86 89 29 56 2 2 2 57 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 56 2 2 59 57 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 19 t = 56 x = Columns 1 through 29 48 15 16 86 89 29 2 2 2 59 57 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 29 2 56 2 59 57 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 c = 20 t = 29 x = Columns 1 through 29 48 15 16 86 89 2 2 56 2 59 57 4 6 27 53 91 29 52 32 9 61 67 18 99 12 37 76 71 85 Column 30 91 x = Columns 1 through 29 48 15 16 86 89 2 29 56 2 59 57 4 ...