Sort in descending order without using built-in sort function

Create a function file that accepts a vector of any length and return a vector whose elements are arranged in descending order. For the function name and arguments use b = dowsort(a) where a is the input vector and b is the output vector. Do not use the MATLAB built-in sort function.
I only have code that has sort function as of the moment.

7 Comments

What have you attempted for your home work?
I think I've got it, here is my code
function b = dowsort(a)
descend = 1;
while descend
descend = 0;
for b = 1:numel(a)-1
if a(b+1) > a(b)
temp = a(b);
a(b) = a(b+1);
a(b+1) = temp;
descend = 1;
end
end
end
disp(a)
end
but when I run it in the command window the output is this:
>> a = [1 2 3 4 5 6 7 8 9 10];
>> b = dowsort(a)
10 9 8 7 6 5 4 3 2 1
b =
9
How can I remove the:
b =
9
Why you want to remove 9?
Because I don't need it. I only need to show that the variables I input is descended.
Can you help me find what's wrong with my code?
The variable that you define as the output argument, b, is returned when you call the function. When you execute a expression without an ending semicolon the results of the expression is printed in the command-window. So in that sense there is nothing wrong about your function, however it might not do what you want...
So how can I fix it? What do I need to do to make it do what I want it to do in the first place?
Is there a simplier code that I can do?
Calm and steady. Read my message carefully. Test and try.
  • If you dont want the message "b = 9" written in the command-window. Then you can remove it
  • Either by taking action to the information in "When you execute a expression without an ending semicolon the results of the expression is printed in the command-window."
  • Or by adressing the how you "variable that you define as the output argument, b".
Functions should in principle return something of interest such that they can be reused again and again. This function you write to pass a programming exercise so that general objective is not that clearly in focus - but is still something you should learn to keep in mind (for what use would you be interested in the value of b?)

Sign in to comment.

Answers (1)

Your code does not satisfy the requirements of the assignment. The problem statement says:
Create a function file that accepts a vector of any length and return a vector whose elements are arranged in descending order.
Your function displays the sorted vector and returns the value of the temporary variable the for loop uses to iterate over the array.
I would omit the disp call because what if the user of your code calls it inside a loop that iterates a thousand or a million times? Do they expect or want your code to display a thousand or a million vectors? Probably not.
Right now your code operates on the vector a that it received as input. That's fine to do inside your function (generally speaking, whatever you do in your function's workspace doesn't affect anything outside its workspace until you return something from your function.) Since your assignment constrains what you're allowed to call the output argument you probably want to assign the vector on which your code operated to the required variable name at the end, just before the function returns.
x = 1:10
y = x % now y is the same as x
If this weren't an assignment I'd probably just use the same variable name as the input and output arguments.
function a = dowsort(a)

Categories

Products

Release

R2019a

Tags

Asked:

on 22 Oct 2021

Answered:

on 22 Oct 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!