How can I return a vector sorting from least to greatest without using the "sort" command?

If you a given a vector v=[4 2 7 4 12 9] how can I return this vector sorted from least to greatest without using the "sort" command. Is there a way of achieving this using loops?

5 commentaires

Adam
Adam le 17 Oct 2014
Modifié(e) : Adam le 17 Oct 2014
Obviously you can write your own sort function of various types in any language, including Matlab. It sounds like an algorithmic problem more so than a Matlab one though. If you can put together a pseudo-code sort algorithm then converting that to Matlab language should not pose any difficulty and people here can help if it does.
Why do you not want to use 'sort' though. It sounds like the type of constraint that would be placed on a homework question so obviously one you are expected to figure out yourself!
What kind of pseudo code can I use for this? I have already tried using the < and > commands and I even tried using for loops. I don't know where I'm going wrong.
Well it depends what your purpose is in doing this. Obviously you can build up code yourself from something basic like:
if v(2) < v(1)
tmp = v(1);
v(1) = v(2);
v(2) = tmp;
end
which swaps the first two elements around. A for or while loop is essentially just a generalisation of an if statement.
Sorting algorithms are amongst the most fundamental algorithms around though even though nowadays most people just use built-in ones instead of reinventing their own.
Any number of websites will no doubt give you either pseudo-code or code in some other language that you can convert to Matlab.
Writing your own algorithm will help you understand better though which I assume is the purpose of doing it without using the built-in sort.
Hi! I would like to know how could you that if I want the vector sorting from GREATEST to LEAST without using the "sort", "min", "max" commands.
Thank you!
Jessica, then you'd have to manually write your own version of sort, min and max. No one here wants to do that since there are already built in functions for that. The only people who say they need to do things manually without using the full power of built-in MATLAB functions are students doing homework assignments. And in that situation of course, we cannot provide the code (even if we did want to write it) because students are not allowed to turn in someone elses work as their own. I'm sure you can find pseudocode for sorting on Wikipedia or elsewhere. So good luck. If you still need help, see the following links:

Connectez-vous pour commenter.

Réponses (3)

Probably the easiest way:
v=[4 2 7 4 12 9];
for k1 = 1:length(v)
[sv(k1),ix] = min(v); % Find Minimum & Index
v(ix) = []; % Set Previous Minimum To Empty
end
v = sv % Output

4 commentaires

Hi Star Strider,
Can you explain how "[sv(k1),ix]" indexes the value? I'm a little confused about that. Also, I came across this code while searching for a solution to the problem I was stumped on.
Thanks!
It goes along building up sv by extracting the min from the vector (or what remains of it). Once the min of all the values has been extracted and stored in sv, it is deleted from the v vector so that it can't be found anymore. So basically at each step you're just pulling out the min of the remaining, and shrinking, vector "v" until all values have been examined and transferred to sv.
could you explain the addition of 'ix' ?

Connectez-vous pour commenter.

One of the guys we play cards with sorts this way. Makes for long games but works for a small number of cards
v = [4 2 7 4 12 9];
while 1
idx = randperm(numel(v));
if issorted(v(idx))
vsort = v(idx);
break
end
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

S
S
le 17 Oct 2014

Commenté :

le 7 Nov 2020

Community Treasure Hunt

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

Start Hunting!

Translated by