Cody

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

Solution 2869202

Submitted on 23 Aug 2020 by Ryan Holman
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))

x = 1 5 4 2 8 acnt = 1 x = 1 4 5 2 8 acnt = 2 x = 1 4 2 5 8 acnt = 3 x = 1 2 4 5 8 acnt = 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))

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

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

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

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

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 acnt = 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 acnt = 2 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 acnt = 3 x = Columns 1 through 29 15 16 48 29 86 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 acnt = 4 x = Columns 1 through 29 15 16 29 48 86 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 acnt = 5 x = Columns 1 through 29 15 16 29 48 86 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 acnt = 6 x = Columns 1 through 29 15 16 29 48 56 86 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 acnt = 7 x = Columns 1 through 29 15 16 29 48 56 86 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 acnt = 8 x = Columns 1 through 29 15 16 29 48 56 2 86 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 acnt = 9 x = Columns 1 through 29 15 16 29 48 2 56 86 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 acnt = 10 x = Columns 1 through 29 15 16 29 2 48 56 86 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 acnt = 11 x = Columns 1 through 29 15 16 2 29 48 56 86 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 acnt = 12 x = Columns 1 through 29 15 2 16 29 48 56 86 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 acnt = 13 x = Columns 1 through 29 2 15 16 29 48 56 86 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 acnt = 14 x = Columns 1 through 29 2 15 16 29 48 56 86 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 acnt = 15 x = Columns 1 through 29 2 15 16 29 48 56 59 86 89 57 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 16 x = Columns 1 through 29 2 15 16 29 48 56 59 86 57 89 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 17 x = Columns 1 through 29 2 15 16 29 48 56 59 57 86 89 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 18 x = Columns 1 through 29 2 15 16 29 48 56 57 59 86 89 2 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 19 x = Columns 1 through 29 2 15 16 29 48 56 57 59 86 2 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 20 x = Columns 1 through 29 2 15 16 29 48 56 57 59 2 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 21 x = Columns 1 through 29 2 15 16 29 48 56 57 2 59 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 22 x = Columns 1 through 29 2 15 16 29 48 56 2 57 59 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 23 x = Columns 1 through 29 2 15 16 29 48 2 56 57 59 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 24 x = Columns 1 through 29 2 15 16 29 2 48 56 57 59 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 25 x = Columns 1 through 29 2 15 16 2 29 48 56 57 59 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 26 x = Columns 1 through 29 2 15 2 16 29 48 56 57 59 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 27 x = Columns 1 through 29 2 2 15 16 29 48 56 57 59 86 89 6 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 28 x = Columns 1 through 29 2 2 15 16 29 48 56 57 59 86 6 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 29 x = Columns 1 through 29 2 2 15 16 29 48 56 57 59 6 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 30 x = Columns 1 through 29 2 2 15 16 29 48 56 57 6 59 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 31 x = Columns 1 through 29 2 2 15 16 29 48 56 6 57 59 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 32 x = Columns 1 through 29 2 2 15 16 29 48 6 56 57 59 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 33 x = Columns 1 through 29 2 2 15 16 29 6 48 56 57 59 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 34 x = Columns 1 through 29 2 2 15 16 6 29 48 56 57 59 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 35 x = Columns 1 through 29 2 2 15 6 16 29 48 56 57 59 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 36 x = Columns 1 through 29 2 2 6 15 16 29 48 56 57 59 86 89 27 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 37 x = Columns 1 through 29 2 2 6 15 16 29 48 56 57 59 86 27 89 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 38 x = Columns 1 through 29 2 2 6 15 16 29 48 56 57 59 27 86 89 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 39 x = Columns 1 through 29 2 2 6 15 16 29 48 56 57 27 59 86 89 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 40 x = Columns 1 through 29 2 2 6 15 16 29 48 56 27 57 59 86 89 53 91 29 52 32 4 61 67 18 99 12 37 76 71 85 91 Column 30 9 acnt = 4...

Suggested Problems

Community Treasure Hunt

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

Start Hunting!