How to bubble sort incorporating deal function

I have the following code:
close all; clear; clc;
reset(RandStream.getGlobalStream,sum(100*clock)); %this is to get a bunch of random numbers
for i=1:100
x(i)=round(rand*50+50);
end
clear i
ans=zeros(1,100,'uint32');
original=x;
swap=0;
num=numel(x);
for j=0:num-1
for i=1:num-j-1
if x(i)> x(i+1)
temp=x(i);
x(i)=x(i+1);
x(i+1)=temp;
swap=swap+1;
end
end
end
I am trying to modify this bubble sorting method to include the deal function but I am not sure how. I know that i should use 'ans' as a matrix full of zeros and use deal to replace those zeros with values. Anyone know what to do, please? Any help would be much appreciated, thank you.

Answers (2)

a = 3; b = -2;
[a,b]
ans = 1×2
3 -2
[a,b] = deal(b,a)
a = -2
b = 3
[a,b]
ans = 1×2
-2 3
However in context,
x = [3, -2]
x = 1×2
3 -2
x(1:2)
ans = 1×2
3 -2
x(1:2) = x([2 1])
x = 1×2
-2 3
x(1:2)
ans = 1×2
-2 3
When you are swapping within a matrix, it is better style to use indexing to do the swapping, instead of using deal()

2 Comments

Thank you, but do you know how to do it with deal? I am trying to accomplish the same result but using the deal function
I showed how to do it with deal as the first option.

Sign in to comment.

There are a number of improvements I'd recommend you make to your code.
close all; clear; clc;
Don't use "close all; clear; clc;" in your code. At least a couple of the MVPs here call that out as "cargo cult programming". clear in particular will prevent you from setting breakpoints in your code if you need to debug.
reset(RandStream.getGlobalStream,sum(100*clock)); %this is to get a bunch of random numbers
Use the rng function with the 'shuffle' option instead.
for i=1:100
x(i)=round(rand*50+50);
end
clear i
You can generate arrays of numbers with one call to the random number generator functions. In this case, I'd recommend using randi instead of round(rand...).
ans=zeros(1,100,'uint32');
original=x;
swap=0;
num=numel(x);
for j=0:num-1
for i=1:num-j-1
if x(i)> x(i+1)
temp=x(i);
x(i)=x(i+1);
x(i+1)=temp;
swap=swap+1;
end
end
end
Are you trying to use deal to avoid needing the temporary variable? Neither the temporary variable nor deal are needed here. As an example, the following code swaps elements n and n+1 in x:
x = 1:10
x = 1×10
1 2 3 4 5 6 7 8 9 10
n = 6;
x([n n+1]) = x([n+1 n])
x = 1×10
1 2 3 4 5 7 6 8 9 10
If you must use deal (for a homework assignment, I assume?) you could. But it's a lot more verbose.
n = 3; % swap 3 and 4
[x(n), x(n+1)] = deal(x(n+1), x(n))
x = 1×10
1 2 4 3 5 7 6 8 9 10
x = 1×10
1 2 4 3 5 7 6 8 9 10

2 Comments

Note that round(rand*constant) and randi([0 constant]) have different statistical properties.
For example, round(rand*3) is round() of a value that is between 0 (exclusive) and 3 (exclusive). 0 (exclusive) to 0.5 (exclusive) will round to 0 -- a width of 1/2 that rounds to zero. 0.5 (inclusive) to 1.5 (exclusive) round to 1 -- a width of 1 that rounds to one. 1.5 (inclusive) to 2.5 (exclusive) round to 2 -- a width of 1 that rounds to two. 2.5 (inclusive) to 3 (exclusive) rounds to 3 -- a width of 1/2 that rounds to three. So 0 and 3 are both probability 1/2 relative to the probabilities for 1 and 2.
But rand([0 3]) would produce 0, 1, 2, and 3 with equal probability, 1/4 each.
That's true. But if this is homework as I hypothesized, I'm guessing the assignment said something like "generate uniformly distributed integers from 50 to 100" in which case randi would be the right choice. For the 0 to 3 case:
n = 1e6;
x1 = round(3*rand(1, n));
x2 = randi([0 3], 1, n);
ax1 = subplot(1, 2, 1);
histogram(x1, Normalization = "probability");
title("rand")
ax2 = subplot(1, 2, 2);
histogram(x2, Normalization = "probability");
title("randi")
% Synchronize the axes labels and show red lines at the three relevant probabilities
linkaxes([ax1, ax2], 'y')
levels = [0, 1/6, 1/4, 1/3];
yline(ax1, levels, 'r:')
yline(ax2, levels, 'r:')
yticks([ax1, ax2], levels)
yticklabels([ax1, ax2], ["0", "1/6", "1/4", "1/3"])

Sign in to comment.

Products

Release

R2022b

Tags

Asked:

on 23 Feb 2023

Commented:

on 24 Feb 2023

Community Treasure Hunt

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

Start Hunting!