Bubble sort for loop
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
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 件のコメント
Guillaume
2019 年 10 月 3 日
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
2019 年 10 月 3 日
Corrected. Thanks.
Matpar
2019 年 10 月 4 日
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
2019 年 10 月 4 日
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
2019 年 10 月 4 日
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
2019 年 10 月 4 日
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
2019 年 10 月 4 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
