Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

50 pts Question 1 a list of integers, sort it as follows normal lexicographical

ID: 2249673 • Letter: 5

Question

50 pts Question 1 a list of integers, sort it as follows normal lexicographical sort order (ie. 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 46813571 Do not use MATLAB's built-in sortis) function for this To test your code, you can make up some data. Here is a suggestion: ata (227125 2 159 19 4 14 8 31 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 hle. Upload Choose a Flle

Explanation / Answer

Code:

clc
clear all
%x=[22 7 -20 3 45 -3 11 6 4 3];
x=input('Enter any range of values in matrix form (e.g: [1 2 3]) : ');
a=0;b=0;
for i=1:length(x)
if(mod(x(i),2)==0) % checking for even numbers
a=a+1;
xe(a)=x(i); %even numbers
else   
b=b+1;
xo(b)=x(i); %odd numbers
end
end
for j=1:1:length(xe)-1
% comparing each number with the next and swapping
for i=1:1:length(xe)-1
if xe(i)>xe(i+1);
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=xe(i);
xe(i)=xe(i+1);
xe(i+1)=temp;
end
end
end

for j=1:1:length(xo)-1
% comparing each number with the next and swapping
for i=1:1:length(xo)-1
if xo(i)>xo(i+1);
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=xo(i);
xo(i)=xo(i+1);
xo(i+1)=temp;
end
end
end
xsorted=[xe xo];
disp('The sorted numbers are: ')
disp(xsorted)

Output:

Enter any range of values in matrix form [1 2 3] : [55 23 -4 -6 -5 221 04 -9 34 76 34 23 77]

The sorted numbers are:
-6 -4 4 34 34 76 -9 -5 23 23 55 77 221
  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote