I should make this code stops sorting when it is already sor
I should make this code stops sorting when it is already sorted
a=[1 10 23 8 99 5 4 10 11 0 15 20 41 22 25 26 49 48 39 29];
n=length(a);
disp(\'Unsorted\');
disp(a);
disp(\'Sorting\');
for j=1:n-1
for i=1:n-1
if a(i)>a(i+1)
tmp=a(i);
a(i)=a(i+1);
a(i+1)=tmp;
end
end
disp(a);
end
end
what should I edit (in MATLAB)
Solution
clc
clear all
a=[1 10 23 8 99 5 4 10 11 0 15 20 41 22 25 26 49 48 39 29];
n=length(a);
disp(\'Unsorted\');
disp(a);
disp(\'Sorting\');
for j=1:n-1
for i=1:n-1
if a(i)>a(i+1)
tmp=a(i);
a(i)=a(i+1);
a(i+1)=tmp;
end
end
end
disp(a);
RESULT:
Unsorted
Columns 1 through 8
1 10 23 8 99 5 4 10
Columns 9 through 16
11 0 15 20 41 22 25 26
Columns 17 through 20
49 48 39 29
Sorting
Columns 1 through 8
0 1 4 5 8 10 10 11
Columns 9 through 16
15 20 22 23 25 26 29 39
Columns 17 through 20
41 48 49 99
![I should make this code stops sorting when it is already sorted a=[1 10 23 8 99 5 4 10 11 0 15 20 41 22 25 26 49 48 39 29]; n=length(a); disp(\'Unsorted\'); dis I should make this code stops sorting when it is already sorted a=[1 10 23 8 99 5 4 10 11 0 15 20 41 22 25 26 49 48 39 29]; n=length(a); disp(\'Unsorted\'); dis](/WebImages/31/i-should-make-this-code-stops-sorting-when-it-is-already-sor-1090518-1761574142-0.webp)
![I should make this code stops sorting when it is already sorted a=[1 10 23 8 99 5 4 10 11 0 15 20 41 22 25 26 49 48 39 29]; n=length(a); disp(\'Unsorted\'); dis I should make this code stops sorting when it is already sorted a=[1 10 23 8 99 5 4 10 11 0 15 20 41 22 25 26 49 48 39 29]; n=length(a); disp(\'Unsorted\'); dis](/WebImages/31/i-should-make-this-code-stops-sorting-when-it-is-already-sor-1090518-1761574142-1.webp)