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
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
2014년 10월 17일
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
2020년 11월 7일
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
2020년 11월 7일
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:
답변 (3개)
Image Analyst
2014년 10월 17일
4 개 추천
Star Strider
2014년 10월 17일
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
Brandon Marlowe
2016년 11월 2일
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
2016년 11월 2일
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
2016년 11월 22일
Ahhhhh, thank you very much!
Diekoloreoluwa Ogundana
2018년 3월 20일
could you explain the addition of 'ix' ?
Sean de Wolski
2014년 10월 17일
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
James Tursa
2016년 11월 22일
LOL. Love this one ...
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!