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

Task 2: Basic computation in a MatLab. Creating MatLab Function (10 pts) There a

ID: 2074127 • Letter: T

Question

Task 2: Basic computation in a MatLab. Creating MatLab Function (10 pts) There are different sorting algorithms available to sort an array. Create and submit a MatLab function called insertion_sort.m according to the specification given. Include this function in your zip archive. General Description: This function takes an array of numbers and sorts them in ascending order (smallest or most negative to largest or most positive). January 26, 2017 Calling Syntax: sorted_list = insertion_sort(unsorted_list) Input Arguments: unsorted_list - array of numbers of length N Return Arguments: sorted_list - array of same numbers sorted in ascending order Additional Specifications and Notes This function must NOT use any built-in MatLab functions for sorting. You are to implement this using flow control statements along with comparison and assignment primitives. Use the Insertion Sort Algorithm for sorting. You can find it at wikipedia.org. The sorted list should be of the same “shape” as the unsorted list. That is, if the input is Nx1, then the output must be Nx1, not 1xN. For this task, you have to verify the type of the input within your code, i.e., check the validity that the input is in a correct format (that the input array contains only numbers).

Explanation / Answer

function sorted_list = bubble_sort(unsorted_list)

tf = isnumeric(unsorted_list); % check the validity that the input is in a correct format (that the input array contains only numbers , (it is not inbuilt lib for sorting )).

if tf==0

disp("Not a valid format of unsorted_list ");

else

[m,n]=size(unsorted_list);

if m==1 %row vector

%bubble sort

for i=1:n-1

for j=1:n-1-i

if unsorted_list(1,j) > unsorted_list(1,j+1)

temp=unsorted_list(1,j);

unsorted_list(1,j)=unsorted_list(1,j+1);

unsorted_list(1,j+1)=temp;

end

end

end

else %column vector to row vector and then sort

unsorted_list=unsorted_list';

[m,n]=size(unsorted_list);

for i=1:n-1

for j=1:n-1-i

if unsorted_list(1,j) > unsorted_list(1,j+1)

temp=unsorted_list(1,j);

unsorted_list(1,j)=unsorted_list(1,j+1);

unsorted_list(1,j+1)=temp;

end

end

end

unsorted_list=unsorted_list';

end

end

sorted_list=unsorted_list; %here answer in sorted_list and if not valid input then answer will be same input list

end

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