Sorting in Descending Order without SORT
3 次查看(过去 30 天)
显示 更早的评论
I have to write a code that sorts a vector WITHOUT using the sort command (only if/for statements) into a descending order. Here is my code so far:
function y = put(x)
y = x(1);
for i = 1:length(x) if y<x(i) y = x(1:length(x)); end; end;
However, the end of the code y=x(1:length(x)) I need it to yield the sorted version of vector x, not just x itself.
0 个评论
回答(3 个)
Jan
2011-4-4
You are not the first person who creates an sorting function. Therefore it will be a good idea to ask Google and Wikipedia at first.
5 个评论
Ned Gulley
2011-4-4
If you think someone answered your question, please accept the answer. That way we know when the question has been answered to your satisfaction and the answerer gets credit for helping you.
Mert Demir
2019-4-22
编辑:Mert Demir
2019-5-20
clc; clear all
N=input('N: ');
for i=1:N
x(i)=input('X;');
end
disp(x);
for j=1:N-1
eb=x(j);es=j;
for i=j+1:N
if x(i)>eb
es=i;
eb=x(i);
end
end
x(es)=x(j); x(j)=eb;
end
disp(x);
This is the code for finding the biggest one do some addingz to it i mean whren you find highest one put it to 1 and delete it from vector and find biggest again şut it to 2 do that i think it can solve problem . yea question is old but that is for the people who looking to it today .
1 个评论
Guillaume
2019-4-22
"A good answer" is debatable.
- No comments whatsoever
- Meaningless variable names
- Not even a mention of which sort algorithm is used (selection sort, one of the simplest but also worse performing sorting algorithm)
Walter Roberson
2011-4-4
The code y = x(1:length(x)) is, for a vector x, the same thing as y = x. Your existing code is thus
y = x(1);
for i = 1:length(x) if y<x(i) y = x; end; end;
On the first iteration, y = x(1), and "i" starts at 1, so the first thing you would be doing in the loop is testing x(1) < x(1) . That will be false because x(1) is equal to x(1) [unless x(1) is NaN] so the body of the "if" will be skipped. The second iteration will test y (still x(1)) against x(2).
Suppose the test x(1) < x(2) is false, looping through another time... and suppose it went on being false to the end, x(1) < x(end) being false. What can we say about the array array x at that point? This: that x(1) >= x(2) >= x(3) and all the way to the end. In such a condition, the array is already in descending order as you want the output to be, so your loop test is correct in-so-far as it knows enough to leave alone an array that is already sorted in descending order.
Suppose, though, that some portion of the test succeeds, such as if x(1) < x(2) . In that case, you set y to be the entire array x and you go on to the next iteration. The next iteration, you test y (now x as a whole) against x(3). That is a vector being tested against a scalar. Each of the elements in the vector will be tested against x(3) and the test will only be considered true if all of the sub-tests are true. That cannot, however, be the case because y(3) = x(3) and x(3) < x(3) is false, so the overall test will fail. By induction the rest of the test loops must also fail, leaving y = x . Which happens to be the same result as if nothing had been done. Your sorting routine thus leaves the data in the same order, always.
Your routine has an element of correctness to it in handling a sorted array without changes, but what happens elsewise needs much more thought.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!