Bubble sort error line 1

4 ビュー (過去 30 日間)
Joe Ainsworth
Joe Ainsworth 2021 年 9 月 12 日
コメント済み: Joe Ainsworth 2021 年 9 月 12 日
Hi, i am currently trying to use a bubble sort for a series of inputed numbers but im getting an error on line 2.
can someone please explain why i am getting this error and how to fix it.
thank you in advance.
here is my code and the error: Error in Bubble1 (line 2)
cases = input('Enter cases with [ ] around them:');
cases = input('Enter cases with [ ] around them:');
sorted_array = bubble_sort(cases)
function array = bubble_sort( array )
for i = 1:length(array)
for j = 1:length(array)-i
if(array(j)>array(j+1))
temp = array(j+1);
array(j+1) = array(j);
array(j) = temp;
end
end
end
end

採用された回答

Chunru
Chunru 2021 年 9 月 12 日
編集済み: Chunru 2021 年 9 月 12 日
You code can run. if you use "input", key in the following "[1 3 2 5 2]" without quotes.
%cases = input('Enter cases with [ ] around them:');
cases = [1 3 2 5 2]
cases = 1×5
1 3 2 5 2
sorted_array = bubble_sort(cases)
sorted_array = 1×5
1 2 2 3 5
function array = bubble_sort( array )
for i = 1:length(array)
for j = 1:length(array)-i
if(array(j)>array(j+1))
temp = array(j+1);
array(j+1) = array(j);
array(j) = temp;
end
end
end
end
  4 件のコメント
Joe Ainsworth
Joe Ainsworth 2021 年 9 月 12 日
@Walter Roberson for whatever reason my code is working perfectly now, before it wouldnt even let me run it saying error on line 2
Joe Ainsworth
Joe Ainsworth 2021 年 9 月 12 日
Thanks though!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by