Filter löschen
Filter löschen

Sorting in Descending Order without SORT

2 Ansichten (letzte 30 Tage)
Ashley Dunn
Ashley Dunn am 3 Apr. 2011
Bearbeitet: Mert Demir am 20 Mai 2019
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.

Antworten (3)

Jan
Jan am 4 Apr. 2011
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.
Another idea would be searching in the FEX and find e.g.: quicksort-learn
  5 Kommentare
Ashley Dunn
Ashley Dunn am 4 Apr. 2011
Sorry. I just thought he was being cheeky. Thanks for the help u guys. I figured it out.
Ned Gulley
Ned Gulley am 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.

Melden Sie sich an, um zu kommentieren.


Mert Demir
Mert Demir am 22 Apr. 2019
Bearbeitet: Mert Demir am 20 Mai 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 Kommentar
Guillaume
Guillaume am 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)

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 4 Apr. 2011
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.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by