Cody

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

Solution 2976691

Submitted on 21 Sep 2020 by Jeremy Perez
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))

n = 5 c = 0 b = 0 x = 1 5 4 2 8 c = 1 b = 1 x = 1 4 5 2 8 c = 2 b = 1 x = 1 4 2 5 8 c = 3 b = 1 b = 0 x = 1 2 4 5 8 c = 4 b = 1 b = 0

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

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

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

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

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

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

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

n = 30 c = 0 b = 0 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 b = 1 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 c = 20 b = 1 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 76 99 71 85 91 Column 30 9 c = 21 b = 1 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 76 71 99 85 91 Column 30 9 c = 22 b = 1 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 76 71 85 99 91 Column 30 9 c = 23 b = 1 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 76 71 85 91 99 Column 30 9 c = 24 b = 1 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 76 71 85 91 9 Column 30 99 c = 25 b = 1 b = 0 x = Columns 1 through 29 15 16 48 29 86 56 2 59 57 2 6 27 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 26 b = 1 x = Columns 1 through 29 15 16 48 29 56 86 2 59 57 2 6 27 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 27 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 86 59 57 2 6 27 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 28 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 86 57 2 6 27 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 29 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 86 2 6 27 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 30 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 86 6 27 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 31 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 86 27 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 32 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 27 86 53 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 33 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 27 53 86 89 29 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 34 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 27 53 86 29 89 52 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 35 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 27 53 86 29 52 89 32 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 36 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 27 53 86 29 52 32 89 4 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 37 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 27 53 86 29 52 32 4 89 61 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 38 b = 1 x = Columns 1 through 29 15 16 48 29 56 2 59 57 2 6 27 53 86 29 52 32 4 61 89 67 18 91 12 37 76 71 85 91 9 Column 30 99 c = 39 b = 1 ...

Suggested Problems

Community Treasure Hunt

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

Start Hunting!