Sorting in Descending Order without SORT
Show older comments
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.
Answers (3)
Jan
on 4 Apr 2011
2 votes
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 Comments
Ashley Dunn
on 4 Apr 2011
Walter Roberson
on 4 Apr 2011
Jan's answer was in no way rude: he indicated where to find what you need.
Your existing code is not even reasonably close to being usable for sorting, so the first thing you need to do is learn how to sort. You aren't just having a problem with syntax: you do not know how to implement a sort and you need to learn that first.
By the way, your existing code violates your stated constraint that only "if/for statements" can be used: you use an assignment statement. That assignment statement contains a call to the function subsref(), so as well as using other statement types you are using other function calls. Your program cannot be completed without at least *one* assignment statement: it is an error for you to not assign something to the output variable.
Jan
on 4 Apr 2011
@Ashley: My answer was not meant rude in any way.
Sorting is not trivial and you can find thousands of scientific papers concerning more than 100 different sorting algorithms. Re-inveting the wheel is not an efficient idea, therefore Gagan has published the mentioned function in the FEX to support people having questions exactly as yours. For other sorting algorithm Wikipedia a is nice source for further information. That's all I wanted to say.
If quicksort does not help you (although it should), which sorthing algorithm do yyou want to implement? Your code example does not clear this in any way.
Ashley Dunn
on 4 Apr 2011
Ned Gulley
on 4 Apr 2011
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
on 22 Apr 2019
Edited: Mert Demir
on 20 May 2019
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 Comment
Guillaume
on 22 Apr 2019
"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
on 4 Apr 2011
0 votes
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.
Categories
Find more on Shifting and Sorting Matrices 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!