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

Write a function with the following header function [sorted] my sort (vector) wh

ID: 3792534 • Letter: W

Question

Write a function with the following header function [sorted] my sort (vector) where: vector is a (possibly empty) row vector of class double. sorted is a row vector of class double that has the same size as vector and that contains the same values as vector, but ordered in increasing order If present in vector, NaN values should be placed at the end of sorted. You may not use Matlab's built-in functions min, max, find, and sort in this question. Hint: You can remove the element of index i from a row or column vector vector of class char, double, or logical, by using the syntax: vector (i) I]. Test cases: [sorted] my sort([0]) sorted [sorted] my sort (12, 1]) sorted [sorted] my sort (12, 1, 9, 10, pij) sorted 10.0000 1.0000 2.0000 3.1416 9.0000 [sorted] my sort (16, 3, 7, 1, 0, 1, 71) sorted

Explanation / Answer

%%create a matlab script file and save as my_sort.m

%%Matlab script that takes an unosorted

%%vector as input and sorts in ascending order

function [sorted] = my_sort(vector)

%%get length of the vector

n=length(vector);

%%set sort

move=0;

k=0;

%%check move is not 1

while ~move

%%set move =1

move=1;

%%increment k

k=k+1;

%%for loop

for j=1:n-k;

%%checking if element at j is greater

%%element at j+1

if vector(j)>vector(j+1)

%swap

temp=vector(j);

vector(j)=vector(j+1);

vector(j+1)=temp;

move=0;

%end of if condition

end

%end of for condition

end

%end of while condition

end

%%set sorted vector to sorted

sorted=vector;

-----------------------------------------------------------------------------

Sample Output:

--> [sorted]=my_sort([0])
sorted =
0
--> [sorted]=my_sort([2,1])
sorted =
1 2
--> [sorted]=my_sort([2,1,9,-10,pi])
sorted =
-10.0000    1.0000    2.0000    3.1416    9.0000
--> [sorted]=my_sort([6,3,7,1,0,1,7])
sorted =
0 1 1 3 6 7 7

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