Cody

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

Solution 3011125

Submitted on 30 Sep 2020 by ivan fernandez
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))

iter = 5 countSwaps = 0 s = 1 x = 5 5 4 2 8 x = 1 5 4 2 8 countSwaps = 1 s = 4 x = 1 5 5 2 8 x = 1 4 5 2 8 countSwaps = 2 s = 2 x = 1 4 5 5 8 x = 1 4 2 5 8 countSwaps = 3 s = 2 x = 1 4 4 5 8 x = 1 2 4 5 8 countSwaps = 4 c = 4

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

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

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

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

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

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

Suggested Problems

Community Treasure Hunt

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

Start Hunting!