Cody

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

Solution 2799325

Submitted on 6 Aug 2020 by Cassidy Fialkiewicz
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))

len = 5 first = 5 second = 1 x = 1 1 4 2 8 x = 1 5 4 2 8 c = 1 first = 5 second = 4 x = 1 4 4 2 8 x = 1 4 5 2 8 c = 2 first = 5 second = 2 x = 1 4 2 2 8 x = 1 4 2 5 8 c = 3 first = 5 second = 8 first = 1 second = 4 first = 4 second = 2 x = 1 2 2 5 8 x = 1 2 4 5 8 c = 4 first = 4 second = 5 first = 1 second = 2 first = 2 second = 4 first = 1 second = 2

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

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

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))

len = 13 first = 73 second = 72 x = 72 72 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 c = 1 first = 73 second = 27 x = 72 27 27 56 16 12 21 12 19 1 9 52 44 x = 72 27 73 56 16 12 21 12 19 1 9 52 44 c = 2 first = 73 second = 56 x = 72 27 56 56 16 12 21 12 19 1 9 52 44 x = 72 27 56 73 16 12 21 12 19 1 9 52 44 c = 3 first = 73 second = 16 x = 72 27 56 16 16 12 21 12 19 1 9 52 44 x = 72 27 56 16 73 12 21 12 19 1 9 52 44 c = 4 first = 73 second = 12 x = 72 27 56 16 12 12 21 12 19 1 9 52 44 x = 72 27 56 16 12 73 21 12 19 1 9 52 44 c = 5 first = 73 second = 21 x = 72 27 56 16 12 21 21 12 19 1 9 52 44 x = 72 27 56 16 12 21 73 12 19 1 9 52 44 c = 6 first = 73 second = 12 x = 72 27 56 16 12 21 12 12 19 1 9 52 44 x = 72 27 56 16 12 21 12 73 19 1 9 52 44 c = 7 first = 73 second = 19 x = 72 27 56 16 12 21 12 19 19 1 9 52 44 x = 72 27 56 16 12 21 12 19 73 1 9 52 44 c = 8 first = 73 second = 1 x = 72 27 56 16 12 21 12 19 1 1 9 52 44 x = 72 27 56 16 12 21 12 19 1 73 9 52 44 c = 9 first = 73 second = 9 x = 72 27 56 16 12 21 12 19 1 9 9 52 44 x = 72 27 56 16 12 21 12 19 1 9 73 52 44 c = 10 first = 73 second = 52 x = 72 27 56 16 12 21 12 19 1 9 52 52 44 x = 72 27 56 16 12 21 12 19 1 9 52 73 44 c = 11 first = 73 second = 44 x = 72 27 56 16 12 21 12 19 1 9 52 44 44 x = 72 27 56 16 12 21 12 19 1 9 52 44 73 c = 12 first = 72 second = 27 x = 27 27 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 c = 13 first = 72 second = 56 x = 27 56 56 16 12 21 12 19 1 9 52 44 73 x = 27 56 72 16 12 21 12 19 1 9 52 44 73 c = 14 first = 72 second = 16 x = 27 56 16 16 12 21 12 19 1 9 52 44 73 x = 27 56 16 72 12 21 12 19 1 9 52 44 73 c = 15 first = 72 second = 12 x = 27 56 16 12 12 21 12 19 1 9 52 44 73 x = 27 56 16 12 72 21 12 19 1 9 52 44 73 c = 16 first = 72 second = 21 x = 27 56 16 12 21 21 12 19 1 9 52 44 73 x = 27 56 16 12 21 72 12 19 1 9 52 44 73 c = 17 first = 72 second = 12 x = 27 56 16 12 21 12 12 19 1 9 52 44 73 x = 27 56 16 12 21 12 72 19 1 9 52 44 73 c = 18 first = 72 second = 19 x = 27 56 16 12 21 12 19 19 1 9 52 44 73 x = 27 56 16 12 21 12 19 72 1 9 52 44 73 c = 19 first = 72 second = 1 x = 27 56 16 12 21 12 19 1 1 9 52 44 73 x = 27 56 16 12 21 12 19 1 72 9 52 44 73 c = 20 first = 72 second = 9 x = 27 56 16 12 21 12 19 1 9 9 52 44 73 x = 27 56 16 12 21 12 19 1 9 72 52 44 73 c = 21 first = 72 second = 52 x = 27 56 16 12 21 12 19 1 9 52 52 44 73 x = 27 56 16 12 21 12 19 1 9 52 72 44 73 c = 22 first = 72 second = 44 x = 27 56 16 12 21 12 19 1 9 52 44 44 73 x = 27 56 16 12 21 12 19 1 9 52 44 72 73 c = 23 first = 27 second = 56 first = 56 second = 16 x = 27 16 16 12 21 12 19 1 9 52 44 72 73 x = 27 16 56 12 21 12 19 1 9 52 44 72 73 c = 24 first = 56 second = 12 x = 27 16 12 12 21 12 19 1 9 52 44 72 73 x = 27 16 12 56 21 12 19 1 9 52 44 72 73 c = 25 first = 56 second = 21 x = 27 16 12 21 21 12 19 1 9 52 44 72 73 x = 27 16 12 21 56 12 19 1 9 52 44 72 73 c = 26 first = 56 second = 12 x = 27 16 12 21 12 12 19 1 9 52 44 72 73 x = 27 16 12 21 12 56 19 1 9 52 44 72 73 c = 27 first = 56 second = 19 x = 27 16 12 21 12 19 19 1 9 52 44 72 73 x = 27 16 12 21 12 19 56 1 9 52 44 72 73 c = 28 first = 56 second = 1 x = 27 16 12 21 12 19 1 1 9 52 44 72 73 x = 27 16 12 21 12 19 1 56 9 52 44 72 73 c = 29 first = 56 second = 9 x = 27 16 12 21 12 19 1 9 9 52 44 72 73 x = 27 16 12 21 12 19 1 9 56 52 44 72 73 c = 30 first = 56 second = 52 x = 27 16 12 21 12 19 1 9 52 52 44 72 73 x = 27 16 12 21 12 19 1 9 52 56 44 72 73 c = 31 first = 56 second = 44 x = 27 16 12 21 12 19 1 9 52 44 44 72 73 x = 27 16 12 21 12 19 1 9 52 44 56 72 73 c = 32 first = 27 second = 16 x = 16 16 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 c = 33 first = 27 second = 12 x = 16 12 12 21 12 19 1 9 52 44 56 72 73 x = 16 12 27 21 12 19 1 9 52 44 56 72 73 c = 34 first = 27 second = 21 x = 16 12 21 21 12 19 1 9 52 44 56 72 73 x = 16 12 21 27 12 19 1 9 52 44 56 72 73 c = 35 first = 27 second = 12 x = 16 12 21 12 12 19 1 9 52 44 56 72 73 x = 16 12 21 12 27 19 1 9 52 44 56 72 73 c = 36 first = 27 second = 19 x = 16 12 21 12 19 19 1 9 52 44 56 72 73 x = 16 12 21 12 19 27 1 9 52 44 56 72 73 c = 37 first = 27 second = 1 x = 16 12 21 12 19 1 1 9 52 44 56 72 73 x = 16 12 21 12 19 1 27 9 52 44 56 72 73 c = 38 first = 27 second = 9 x = 16 12 21 12 19 1 9 9 52 44 56 72 73 x = 16 12 21 12 19 1 9 27 52 44 56 72 73 c = 39 first = 27 second = 52 first = 52 second = 44 x = 16 12 21 12 19 1 9 27 44 44 56 72 73 x = 16 12 21 12 19 1 9 27 44 52 56 72 73 c = 40 first = 16 second = 12 x = 12 12 21 12 19 1 9 27 44 52 56 72 73 x = 12 16 21 12 19 1 9 27 44 52 56 72 73 c = 41 first = 16 second = 21 first = 21 second = 12 x = 12 16 12 12 19 1 9 27 44 52 56 72 73 x = 12 16 12 21 19 1 9 27 44 52 56 72 73 c = 42 first = 21 second = 19 x = 12 16 12 19 19 1 9 27 44 52 56 72 73 x = 12 16 12 19 21 1 9 27 44 52 56 72 73 c = 43 first = 21 second = 1 x = 12 16 12 19 1 1 9 27 44 52 56 72 73 x = 12 16 12 19 1 21 9 27 44 52 56 72 73 c = 44 first = 21 second = 9 x = 12 16 12 ...

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

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

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))

len = 30 first = 48 second = 15 x = Columns 1 through 29 15 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 Column 30 9 x = Columns 1 through 29 15 48 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 Column 30 9 c = 1 first = 48 second = 16 x = Columns 1 through 29 15 16 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 Column 30 9 x = Columns 1 through 29 15 16 48 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 Column 30 9 c = 2 first = 48 second = 86 first = 86 second = 89 first = 89 second = 29 x = Columns 1 through 29 15 16 48 86 29 29 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 89 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 3 first = 89 second = 56 x = Columns 1 through 29 15 16 48 86 29 56 56 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 89 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 4 first = 89 second = 2 x = Columns 1 through 29 15 16 48 86 29 56 2 2 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 89 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 5 first = 89 second = 59 x = Columns 1 through 29 15 16 48 86 29 56 2 59 59 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 89 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 6 first = 89 second = 57 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 89 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 7 first = 89 second = 2 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 8 first = 89 second = 6 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 9 first = 89 second = 27 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 89 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 10 first = 89 second = 53 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 11 first = 89 second = 91 first = 91 second = 29 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 91 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 12 first = 91 second = 52 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 91 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 13 first = 91 second = 32 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 91 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 14 first = 91 second = 4 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 91 61 67 18 99 12 37 76 71 85 91 Column 30 9 c = 15 first = 91 second = 61 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 61 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 91 67 18 99 12 37 76 71 85 91 Column 30 9 c = 16 first = 91 second = 67 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 67 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 91 18 99 12 37 76 71 85 91 Column 30 9 c = 17 first = 91 second = 18 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 18 18 99 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 18 91 99 12 37 76 71 85 91 Column 30 9 c = 18 first = 91 second = 99 first = 99 second = 12 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 18 91 12 12 37 76 71 85 91 Column 30 9 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 18 91 12 99 37 76 71 85 91 Column 30 9 c = 19 first = 99 second = 37 x = Columns 1 through 29 15 16 48 86 29 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 18 91 12 37 37 76 71 85 91 ...

Suggested Problems

Community Treasure Hunt

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

Start Hunting!