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

Problem 2 (45 points) Write a function vector sort that takes an arbitrary-lengt

ID: 3802409 • Letter: P

Question

Problem 2 (45 points) Write a function vector sort that takes an arbitrary-length vector as input, sorts the elements in ascending or descending order, depending on the second input (which should be a string) ,and returns the sorted vector. If the second input is anything other than ascending' or descending the function should print a warning and return the input vector unchanged. Use nested conditional structures and loops as necessary. (Do NOT use the built-in MATLAB function sort.) FOLLOW THE PROGRAM DESIGN STEPS DISCUSSED IN THE LECTURE. (Steps 2.-4. must be submitted with your solution; they can be done on paper or in a text editor) STEP 2. Describe your sorting algorithm in detail. Use a variant of"Bubble Sort" Chttps://en wikipedia wiki/Bubble sort), "Selection Sort" (https://en wikipedia.orgwiki/Selection sort), or "Insertion Sort s://en.wikipedia.org/wiki/Insertion sort STEP 3. Specify input/output arguments and program scope. STEP 4. Write a detailed structure plan implementing your algorithm that satisfies the program. scope you specified. STEP 5. Write the function vector sort based on YOUR structure plan. STEP 6. Verify /validate your program. Use your function to sort the following vectors and return/output the sorted vector: a R 7 -4 9 10 j, ascending' AND 'descending b 0 -1 3 4 5 'ascending' AND 'descending -1 6 2 7 'ascending AND descending d 4.5 16.12 10.1 10 -16.11 14 -3 21, ascending AND 'descending a 7 4 -4 9 10 1, up

Explanation / Answer

% Using Variant of Bubble Sort for Sorting Purpose
% Let an array contains 5 elements. A = {1,5,8,0,2}
% Now Bubble Sort Algorithm
% Compare ( A[1] and A[2] ), ( A[2] and A[3] ), ( A[3] and A[4] ), ( A[4] and A[5] ) and 1 element is in its place, Total Comparisons = 4 . Let A[1] Element is fixed then
% Compare ( A[2] and A[3] ), ( A[3] and A[4] ) , ( A[4] and A[5] ) and 2 elements are in its place, Total Comparisons = 3 . Let A[1] and A[2] Element are fixed then
% Compare ( A[3] and A[4] ) , ( A[4] and A[5] ) and 3 elements are in its place, Total Comparisons = 2 . Let A[1], A[2] and A[3] Element are fixed then
% Compare ( A[4] and A[5] ) and all elements are in its place, Total Comparisons = 1

% Input Arguments 1. vect = input unsorted vector, 2. Type of Sorting ascending and descending
% Output Argument vect = Sorted vector
function vect = vector_sort(vect,type)
if(~(strcmp(type,'ascending') || strcmp(type,'descending'))) % Check that input type is other than ascending and descending
disp('Wrong Sort Type');
return;
end
vect_length=numel(vect); % input vector length
for i=1:vect_length-1 % To select an position
for j=1:vect_length-i % To select comparison position
if(strcmp(type,'ascending')) % Check that ascending or descending
if(vect(j)>vect(j+1))
temp=vect(j+1); % assign into temp
vect(j+1)=vect(j);
vect(j)=temp;
end
elseif(strcmp(type,'descending'))
if(vect(j)<vect(j+1))
temp=vect(j+1);
vect(j+1)=vect(j);
vect(j)=temp;
end
end
end
end
  
end

Output:

>> a=[7 4 -4 9 -10];
b=[0 1 3 4 5];
c=[-1 6 2 2 -7];
d=[4.5 -16.12 10.1 10 -16.11 14 -3 2];
>> vector_sort(a,'ascending')

ans =

-10 -4 4 7 9

>> vector_sort(a,'descending')

ans =

9 7 4 -4 -10

>> vector_sort(b,'ascending')

ans =

0 1 3 4 5

>> vector_sort(b,'descending')

ans =

5 4 3 1 0

>> vector_sort(c,'ascending')

ans =

-7 -1 2 2 6

>> vector_sort(c,'descending')

ans =

6 2 2 -1 -7

>> vector_sort(d,'ascending')

ans =

-16.1200 -16.1100 -3.0000 2.0000 4.5000 10.0000 10.1000 14.0000

>> vector_sort(d,'descending')

ans =

14.0000 10.1000 10.0000 4.5000 2.0000 -3.0000 -16.1100 -16.1200

>> vector_sort(a,'up')
Wrong Sort Type

ans =

7 4 -4 9 -10

>>

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