Bubble sort for loop
显示 更早的评论
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!
采纳的回答
Ajay Kumar
2019-10-3
编辑:Ajay Kumar
2019-10-3
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 个评论
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]
Corrected. Thanks.
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!
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
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')
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.
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
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
