so I have one way to do this problem in matlab and it works %100, but I need ano
ID: 2249967 • Letter: S
Question
so I have one way to do this problem in matlab and it works %100, but I need another way to answer this question
this is the code that we got plz help me find another way!
Given a list of integers, sort it as follows: normal lexicographical sort order (i.e. 2 before 3), but any odd number is greater than any even number. For example, if the input is the list 1:8, the answer should be [2 4 6 81 3 5 7]. Do not use MATLAB's built-in sort(s) function for this. To test your code, you can make up some data. Here is a suggestion: data =[22 7 12 5 2 15 9 19 4 14 8 21 18 11 16 1] Submit either a script which demonstrates your code, or a function file named weirdSort with one argument (vector of unsorted numbers) that returns the sorted vector. In either case, submit one .m fileExplanation / Answer
Another way is to first sort out the ascending order and then separate even and odd
MATLAB CODE :
clc;
clear all;
%x=input('Enter the array ');
%---------------sorting ---------------
x=[1 3 5 4 8 9 1];
for j=1:length(x)-1
for i=1:length(x)-1
if x(i)>x(i+1)
xtemp=x(i);
x(i)=x(i+1);
x(i+1)=xtemp;
end
end
end
%----------separating even and odd-------------
a=0;b=0;
for i=1:length(x)
if mod(x(i),2)==0
a=a+1;
xe(a)=x(i);
else
b=b+1;
xo(b)=x(i);
end
end
xsorted=[xe xo];
disp('The sorted array : ');
disp(xsorted);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.