 QUESTION

# Write a function that accepts a variable number of input values and returns the sorted values as an array. The function will be called, for example,...

Write a function that accepts a variable number of input values and returns the sorted values as an array. The function will be called, for example, like this:

b = msort(2,1,4,3)

and will return a vector b=[1 2 3 4] (either row or column vector is OK)

The function should have the following features:

- (10 pts) Help text, including H1 (first line)

- (30 pts) The function should sort in ascending order, i.e., from lowest to highest.

- (20 pts) Optionally include a string parameter ‘d’ or ‘D’ to change the sort order to descending, i.e., highest to lowest. It should be placed as the last argument to the function, i.e., b=msort(2,1,4,3,’d’) returns [4 3 2 1].

- (20 pts) Have an optional second return argument that includes the number of swaps made during the sort process. If no sorting is done, the number of swaps will be zero. The function is then called as [b,n] = msort(2,1,4,3); here b is assigned the sorted value as above, and n is the number of swaps that occurred during the sorting process.

- (10 pts) Should work for only a single input argument, i.e., msort(3) returns 3, rather than generating an error.

- (10 pts) Issue an error if anything other than a number or ‘d’ or ‘D’ is sent as an argument.

Useful functions:

lower() – returns lower case of a string or character: lower(‘D’)=’d’

isnumeric() – returns TRUE (1) if argument is a number, else returns FALSE (0)

ischar() – returns TRUE(1) if argument is a character or string, else returns FALSE(0)

Run your function on the following lists and include both the final sort value and the number of swaps:

a) (10 pts) 1, 2,3,4,5,6,7,8,9,10 both ascending and descending

b) (20 pts) Your ID number, using each digit as a separate argument (9 total), i.e., 106-321- 987 would be called as msort(1,0,6,3,2,1,9,8,7)

c) (20 pts) Call the function with ten random numbers. You can either make them up, compute them ahead of time and copy them in, or generate them on the fly as follows: msort(rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand())