Hi can a professional guide me in terms of a bubble sort using 2 for loops please!
I have been googling to help myself but it's challenging to understand how they actually work! I have no code!
Thanx in advance for assisting me and responding to my questions!

 Respuesta aceptada

Ajay Kumar
Ajay Kumar el 3 de Oct. de 2019
Editada: Ajay Kumar el 3 de Oct. de 2019

3 votos

First try to understand the sorting algorithm. There are many videos on youtube that explains bubble sort.
Your data being x.
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;
end
end
end

7 comentarios

Guillaume
Guillaume el 3 de Oct. de 2019
Also, 2 for loops are not required for bubble sorting.
Well, you'ver replaced one of the for loop by a while loop. for and while loops are more or less equivalent. You can always rewrite one in term of the other.
However, note that your algorithm does not work properly (the while condition is very incorrect). E.g it will fail to sort properly
x = [1 3 2]
%or
x = [3 1 4 2]
Ajay Kumar
Ajay Kumar el 3 de Oct. de 2019
Corrected. Thanks.
Matpar
Matpar el 4 de Oct. de 2019
Ok guys,
I was of the opinion that the for loop was the solution! now i am confused with the while loop!
please guide me!
thanks in advance!
Rik
Rik el 4 de Oct. de 2019
Then look at the edited answer.
The point of bubble sort is to loop through all elements. For each element look at all remaining elements on one side and swap them if necessary. I find it more intuitive to loop from 1, so like the code below. (written on mobile, untested)
for n=1:(numel(x)-1)
for m=(n+1):numel(x)
if x(m) > x(n)
x([n m])=x([m n]);
end
end
end
Matpar
Matpar el 4 de Oct. de 2019
forgive me I tired and well I am still here trying I manage to get the understnding going via the manaual way!
This is where i've gotten;
%% Exhaustive Search Implementation
%Imaginative Points Specified on the X Y plane of 2 dimensional Image
% Point W,X,Y,Z, the distance between W & Y as well as Z & W
w=[4,9]; %
x=[1,5]; %
y=[7,2]; %
z=[3,5]; %
wy=[11,3];
zw=[4,1];
%% Specified the distance of each point utilising the Euclidean Distance
%euclideanDistance = sqrt((w2-w1)^2+(x2-x1)^2+(y2-y1)^2+(z2-z1)^2);
%w>x>y>z>xz>wy
ed_wxyz = sqrt((9-4)^2+(5-1)^2 +(2-7)^2+(5-3)^2+(3-11)^2+(1-4)^2);
%% Specified Each Point Individually For Cross Validation Purposes
edw_x = sqrt((9-4)^2+(5-1)^2);
edx_y = sqrt((5-1)^2+(2-7)^2);
edy_z = sqrt((2-7)^2+(5-3)^2);
edz_w = sqrt((5-3)^2+(9-4)^2);
edx_z = sqrt((5-1)^2+(5-3)^2);
edw_y = sqrt((9-4)^2+(2-7)^2);
%% Created Variable T to Sort All Point In Descending Order
t = [edw_x,edx_y,edy_z,edz_w,edx_z,edw_y,ed_wxyz];
sortpoints = sort(t,'descend');
%% Display All Point Individually
disp('The Euclidean Distance of the points are=>');
disp('edw_x'); disp(edw_x);
disp('edx_y'); disp(edx_y);
disp('edy_z'); disp(edy_z);
disp('edz_w'); disp(edz_w);
disp('edx_z'); disp(edx_z);
disp('edw_y'); disp(edw_y);
disp('edw_x'); disp(ed_wxyz);
disp('Here are the points in **RANDOM** order');
%% Display Sorted Points In Descending Order From Matlab Built-in Sort Fucntion
disp(t);
disp('Here are the points in **DESCENDING** order');
%%
% *************************************************************************************
disp(sortpoints);
%%
% *************************************************************************************
disp('This point (X_Z) is the lowest calculation=> 4.4721')
Rik
Rik el 4 de Oct. de 2019
This code has nothing to do with your question. The sort function doesn't use bubble sort, because there are much more efficient algorithms for sorting.
Guillaume
Guillaume el 4 de Oct. de 2019
Indeed what has that code to do with the initial question?
Note that in each of the proposed code it would be more efficient to stop as soon as the inner loop has done a pass without any swapping, rather than continue scanning the whole array.
"I was of the opinion that the for loop was the solution"
There's not much difference between a for loop and a while loop. That was my point to Kumar. You can always rewrite a for loop as a while loop and vice-versa
for i = 1:10
%do something
end
is equivalent to:
i = 1;
while i <= 10
%do something
i = i+1;
end
while:
while somecondition
%do something
end
is equivalent to
for i = 1:Inf
if somecondition
break;
end
%do something
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Oct. de 2019

Comentada:

el 4 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by