How can I return a vector sorting from least to greatest without using the "sort" command?
Show older comments
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 Comments
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!
S
on 17 Oct 2014
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.
Jessica Avellaneda
on 7 Nov 2020
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!
Image Analyst
on 7 Nov 2020
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:
Answers (3)
Image Analyst
on 17 Oct 2014
4 votes
Star Strider
on 17 Oct 2014
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 Comments
Brandon Marlowe
on 2 Nov 2016
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!
Image Analyst
on 2 Nov 2016
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.
Brandon Marlowe
on 22 Nov 2016
Ahhhhh, thank you very much!
Diekoloreoluwa Ogundana
on 20 Mar 2018
could you explain the addition of 'ix' ?
Sean de Wolski
on 17 Oct 2014
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
1 Comment
James Tursa
on 22 Nov 2016
LOL. Love this one ...
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!