Cody

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

Solution 1058673

Submitted on 22 Nov 2016 by Zikobrelli
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))

c = 5 1 4 2 8 j = 0 c = 1 1 4 2 8 c = 1 5 4 2 8 x = 1 5 4 2 8 j = 1 c = 1 4 4 2 8 c = 1 4 5 2 8 x = 1 4 5 2 8 j = 2 c = 1 4 2 2 8 c = 1 4 2 5 8 x = 1 4 2 5 8 j = 3 c = 1 2 2 5 8 c = 1 2 4 5 8 x = 1 2 4 5 8 j = 4

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

c = 7 9 17 4 19 3 17 13 20 18 j = 0 c = 7 9 4 4 19 3 17 13 20 18 c = 7 9 4 17 19 3 17 13 20 18 x = 7 9 4 17 19 3 17 13 20 18 j = 1 c = 7 9 4 17 3 3 17 13 20 18 c = 7 9 4 17 3 19 17 13 20 18 x = 7 9 4 17 3 19 17 13 20 18 j = 2 c = 7 9 4 17 3 17 17 13 20 18 c = 7 9 4 17 3 17 19 13 20 18 x = 7 9 4 17 3 17 19 13 20 18 j = 3 c = 7 9 4 17 3 17 13 13 20 18 c = 7 9 4 17 3 17 13 19 20 18 x = 7 9 4 17 3 17 13 19 20 18 j = 4 c = 7 9 4 17 3 17 13 19 18 18 c = 7 9 4 17 3 17 13 19 18 20 x = 7 9 4 17 3 17 13 19 18 20 j = 5 c = 7 4 4 17 3 17 13 19 18 20 c = 7 4 9 17 3 17 13 19 18 20 x = 7 4 9 17 3 17 13 19 18 20 j = 6 c = 7 4 9 3 3 17 13 19 18 20 c = 7 4 9 3 17 17 13 19 18 20 x = 7 4 9 3 17 17 13 19 18 20 j = 7 c = 7 4 9 3 17 13 13 19 18 20 c = 7 4 9 3 17 13 17 19 18 20 x = 7 4 9 3 17 13 17 19 18 20 j = 8 c = 7 4 9 3 17 13 17 18 18 20 c = 7 4 9 3 17 13 17 18 19 20 x = 7 4 9 3 17 13 17 18 19 20 j = 9 c = 4 4 9 3 17 13 17 18 19 20 c = 4 7 9 3 17 13 17 18 19 20 x = 4 7 9 3 17 13 17 18 19 20 j = 10 c = 4 7 3 3 17 13 17 18 19 20 c = 4 7 3 9 17 13 17 18 19 20 x = 4 7 3 9 17 13 17 18 19 20 j = 11 c = 4 7 3 9 13 13 17 18 19 20 c = 4 7 3 9 13 17 17 18 19 20 x = 4 7 3 9 13 17 17 18 19 20 j = 12 c = 4 3 3 9 13 17 17 18 19 20 c = 4 3 7 9 13 17 17 18 19 20 x = 4 3 7 9 13 17 17 18 19 20 j = 13 c = 3 3 7 9 13 17 17 18 19 20 c = 3 4 7 9 13 17 17 18 19 20 x = 3 4 7 9 13 17 17 18 19 20 j = 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))

c = 73 72 27 56 16 12 21 12 19 1 9 52 44 j = 0 c = 72 72 27 56 16 12 21 12 19 1 9 52 44 c = 72 73 27 56 16 12 21 12 19 1 9 52 44 x = 72 73 27 56 16 12 21 12 19 1 9 52 44 j = 1 c = 72 27 27 56 16 12 21 12 19 1 9 52 44 c = 72 27 73 56 16 12 21 12 19 1 9 52 44 x = 72 27 73 56 16 12 21 12 19 1 9 52 44 j = 2 c = 72 27 56 56 16 12 21 12 19 1 9 52 44 c = 72 27 56 73 16 12 21 12 19 1 9 52 44 x = 72 27 56 73 16 12 21 12 19 1 9 52 44 j = 3 c = 72 27 56 16 16 12 21 12 19 1 9 52 44 c = 72 27 56 16 73 12 21 12 19 1 9 52 44 x = 72 27 56 16 73 12 21 12 19 1 9 52 44 j = 4 c = 72 27 56 16 12 12 21 12 19 1 9 52 44 c = 72 27 56 16 12 73 21 12 19 1 9 52 44 x = 72 27 56 16 12 73 21 12 19 1 9 52 44 j = 5 c = 72 27 56 16 12 21 21 12 19 1 9 52 44 c = 72 27 56 16 12 21 73 12 19 1 9 52 44 x = 72 27 56 16 12 21 73 12 19 1 9 52 44 j = 6 c = 72 27 56 16 12 21 12 12 19 1 9 52 44 c = 72 27 56 16 12 21 12 73 19 1 9 52 44 x = 72 27 56 16 12 21 12 73 19 1 9 52 44 j = 7 c = 72 27 56 16 12 21 12 19 19 1 9 52 44 c = 72 27 56 16 12 21 12 19 73 1 9 52 44 x = 72 27 56 16 12 21 12 19 73 1 9 52 44 j = 8 c = 72 27 56 16 12 21 12 19 1 1 9 52 44 c = 72 27 56 16 12 21 12 19 1 73 9 52 44 x = 72 27 56 16 12 21 12 19 1 73 9 52 44 j = 9 c = 72 27 56 16 12 21 12 19 1 9 9 52 44 c = 72 27 56 16 12 21 12 19 1 9 73 52 44 x = 72 27 56 16 12 21 12 19 1 9 73 52 44 j = 10 c = 72 27 56 16 12 21 12 19 1 9 52 52 44 c = 72 27 56 16 12 21 12 19 1 9 52 73 44 x = 72 27 56 16 12 21 12 19 1 9 52 73 44 j = 11 c = 72 27 56 16 12 21 12 19 1 9 52 44 44 c = 72 27 56 16 12 21 12 19 1 9 52 44 73 x = 72 27 56 16 12 21 12 19 1 9 52 44 73 j = 12 c = 27 27 56 16 12 21 12 19 1 9 52 44 73 c = 27 72 56 16 12 21 12 19 1 9 52 44 73 x = 27 72 56 16 12 21 12 19 1 9 52 44 73 j = 13 c = 27 56 56 16 12 21 12 19 1 9 52 44 73 c = 27 56 72 16 12 21 12 19 1 9 52 44 73 x = 27 56 72 16 12 21 12 19 1 9 52 44 73 j = 14 c = 27 56 16 16 12 21 12 19 1 9 52 44 73 c = 27 56 16 72 12 21 12 19 1 9 52 44 73 x = 27 56 16 72 12 21 12 19 1 9 52 44 73 j = 15 c = 27 56 16 12 12 21 12 19 1 9 52 44 73 c = 27 56 16 12 72 21 12 19 1 9 52 44 73 x = 27 56 16 12 72 21 12 19 1 9 52 44 73 j = 16 c = 27 56 16 12 21 21 12 19 1 9 52 44 73 c = 27 56 16 12 21 72 12 19 1 9 52 44 73 x = 27 56 16 12 21 72 12 19 1 9 52 44 73 j = 17 c = 27 56 16 12 21 12 12 19 1 9 52 44 73 c = 27 56 16 12 21 12 72 19 1 9 52 44 73 x = 27 56 16 12 21 12 72 19 1 9 52 44 73 j = 18 c = 27 56 16 12 21 12 19 19 1 9 52 44 73 c = 27 56 16 12 21 12 19 72 1 9 52 44 73 x = 27 56 16 12 21 12 19 72 1 9 52 44 73 j = 19 c = 27 56 16 12 21 12 19 1 1 9 52 44 73 c = 27 56 16 12 21 12 19 1 72 9 52 44 73 x = 27 56 16 12 21 12 19 1 72 9 52 44 73 j = 20 c = 27 56 16 12 21 12 19 1 9 9 52 44 73 c = 27 56 16 12 21 12 19 1 9 72 52 44 73 x = 27 56 16 12 21 12 19 1 9 72 52 44 73 j = 21 c = 27 56 16 12 21 12 19 1 9 52 52 44 73 c = 27 56 16 12 21 12 19 1 9 52 72 44 73 x = 27 56 16 12 21 12 19 1 9 52 72 44 73 j = 22 c = 27 56 16 12 21 12 19 1 9 52 44 44 73 c = 27 56 16 12 21 12 19 1 9 52 44 72 73 x = 27 56 16 12 21 12 19 1 9 52 44 72 73 j = 23 c = 27 16 16 12 21 12 19 1 9 52 44 72 73 c = 27 16 56 12 21 12 19 1 9 52 44 72 73 x = 27 16 56 12 21 12 19 1 9 52 44 72 73 j = 24 c = 27 16 12 12 21 12 19 1 9 52 44 72 73 c = 27 16 12 56 21 12 19 1 9 52 44 72 73 x = 27 16 12 56 21 12 19 1 9 52 44 72 73 j = 25 c = 27 16 12 21 21 12 19 1 9 52 44 72 73 c = 27 16 12 21 56 12 19 1 9 52 44 72 73 x = 27 16 12 21 56 12 19 1 9 52 44 72 73 j = 26 c = 27 16 12 21 12 12 19 1 9 52 44 72 73 c = 27 16 12 21 12 56 19 1 9 52 44 72 73 x = 27 16 12 21 12 56 19 1 9 52 44 72 73 j = 27 c = 27 16 12 21 12 19 19 1 9 52 44 72 73 c = 27 16 12 21 12 19 56 1 9 52 44 72 73 x = 27 16 12 21 12 19 56 1 9 52 44 72 73 j = 28 c = 27 16 12 21 12 19 1 1 9 52 44 72 73 c = 27 16 12 21 12 19 1 56 9 52 44 72 73 x = 27 16 12 21 12 19 1 56 9 52 44 72 73 j = 29 c = 27 16 12 21 12 19 1 9 9 52 44 72 73 c = 27 16 12 21 12 19 1 9 56 52 44 72 73 x = 27 16 12 21 12 19 1 9 56 52 44 72 73 j = 30 c = 27 16 12 21 12 19 1 9 52 52 44 72 73 c = 27 16 12 21 12 19 1 9 52 56 44 72 73 x = 27 16 12 21 12 19 1 9 52 56 44 72 73 j = 31 c = 27 16 12 21 12 19 1 9 52 44 44 72 73 c = 27 16 12 21 12 19 1 9 52 44 56 72 73 x = 27 16 12 21 12 19 1 9 52 44 56 72 73 j = 32 c = 16 16 12 21 12 19 1 9 52 44 56 72 73 c = 16 27 12 21 12 19 1 9 52 44 56 72 73 x = 16 27 12 21 12 19 1 9 52 44 56 72 73 j = 33 c = 16 12 12 21 12 19 1 9 52 44 56 72 73 c = 16 12 27 21 12 19 1 9 52 44 56 72 73 x = 16 12 27 21 12 19 1 9 52 44 56 72 73 j = 34 c = 16 12 21 21 12 19 1 9 52 44 56 72 73 c = 16 12 21 27 12 19 1 9 52 44 56 72 73 x = 16 12 21 27 12 19 1 9 52 44 56 72 73 j = 35 c = 16 12 21 12 12 19 1 9 52 44 56 72 73 c = 16 12 21 12 27 19 1 9 52 44 56 72 73 x = 16 12 21 12 27 19 1 9 52 44 56 72 73 j = 36 c = 16 12 21 12 19 19 1 9 52 44 56 72 73 c = 16 12 2...

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

c = 4 15 2 5 1 5 8 6 12 13 22 7 j = 0 c = 4 2 2 5 1 5 8 6 12 13 22 7 c = 4 2 15 5 1 5 8 6 12 13 22 7 x = 4 2 15 5 1 5 8 6 12 13 22 7 j = 1 c = 4 2 5 5 1 5 8 6 12 13 22 7 c = 4 2 5 15 1 5 8 6 12 13 22 7 x = 4 2 5 15 1 5 8 6 12 13 22 7 j = 2 c = 4 2 5 1 1 5 8 6 12 13 22 7 c = 4 2 5 1 15 5 8 6 12 13 22 7 x = 4 2 5 1 15 5 8 6 12 13 22 7 j = 3 c = 4 2 5 1 5 5 8 6 12 13 22 7 c = 4 2 5 1 5 15 8 6 12 13 22 7 x = 4 2 5 1 5 15 8 6 12 13 22 7 j = 4 c = 4 2 5 1 5 8 8 6 12 13 22 7 c = 4 2 5 1 5 8 15 6 12 13 22 7 x = 4 2 5 1 5 8 15 6 12 13 22 7 j = 5 c = 4 2 5 1 5 8 6 6 12 13 22 7 c = 4 2 5 1 5 8 6 15 12 13 22 7 x = 4 2 5 1 5 8 6 15 12 13 22 7 j = 6 c = 4 2 5 1 5 8 6 12 12 13 22 7 c = 4 2 5 1 5 8 6 12 15 13 22 7 x = 4 2 5 1 5 8 6 12 15 13 22 7 j = 7 c = 4 2 5 1 5 8 6 12 13 13 22 7 c = 4 2 5 1 5 8 6 12 13 15 22 7 x = 4 2 5 1 5 8 6 12 13 15 22 7 j = 8 c = 4 2 5 1 5 8 6 12 13 15 7 7 c = 4 2 5 1 5 8 6 12 13 15 7 22 x = 4 2 5 1 5 8 6 12 13 15 7 22 j = 9 c = 2 2 5 1 5 8 6 12 13 15 7 22 c = 2 4 5 1 5 8 6 12 13 15 7 22 x = 2 4 5 1 5 8 6 12 13 15 7 22 j = 10 c = 2 4 1 1 5 8 6 12 13 15 7 22 c = 2 4 1 5 5 8 6 12 13 15 7 22 x = 2 4 1 5 5 8 6 12 13 15 7 22 j = 11 c = 2 4 1 5 5 6 6 12 13 15 7 22 c = 2 4 1 5 5 6 8 12 13 15 7 22 x = 2 4 1 5 5 6 8 12 13 15 7 22 j = 12 c = 2 4 1 5 5 6 8 12 13 7 7 22 c = 2 4 1 5 5 6 8 12 13 7 15 22 x = 2 4 1 5 5 6 8 12 13 7 15 22 j = 13 c = 2 1 1 5 5 6 8 12 13 7 15 22 c = 2 1 4 5 5 6 8 12 13 7 15 22 x = 2 1 4 5 5 6 8 12 13 7 15 22 j = 14 c = 2 1 4 5 5 6 8 12 7 7 15 22 c = 2 1 4 5 5 6 8 12 7 13 15 22 x = 2 1 4 5 5 6 8 12 7 13 15 22 j = 15 c = 1 1 4 5 5 6 8 12 7 13 15 22 c = 1 2 4 5 5 6 8 12 7 13 15 22 x = 1 2 4 5 5 6 8 12 7 13 15 22 j = 16 c = 1 2 4 5 5 6 8 7 7 13 15 22 c = 1 2 4 5 5 6 8 7 12 13 15 22 x = 1 2 4 5 5 6 8 7 12 13 15 22 j = 17 c = 1 2 4 5 5 6 7 7 12 13 15 22 c = 1 2 4 5 5 6 7 8 12 13 15 22 x = 1 2 4 5 5 6 7 8 12 13 15 22 j = 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))

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

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!