Hello! I am trying to create a script in which I sort an array in descending order without using any sort function. So far, I came up with this:
A = [2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 18 17 20 19];
n = 1;
while n <= length(A)
if A(n) < A(n+1)
tmp = A(n);
A(n) = A(n+1);
A(n+1) = tmp;
end
n = n +1;
end
I thought it would be this simple, but it apparently isn't. One of the things I do not know how to do is place a value of one array into a new array, which I feel is vital in a script like this, but I did not know how to do it, so I did not implement it. Any help will be appreciated. Thank you in advance!

 Akzeptierte Antwort

Star Strider
Star Strider am 31 Okt. 2015

1 Stimme

Assigning one array to another is like assigning any other variable. Consider the B = A assignment here.
It looks as though you want to do a bubble sort, the simplest sorting algorithm. You’re close. This is how I would do it:
A = [2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 18 17 20 19];
B = A; % Original Vector
swap = true;
while swap
swap = false;
for n = 1:length(A)-1
if A(n) < A(n+1)
tmp = A(n);
A(n) = A(n+1);
A(n+1) = tmp;
swap = true;
end
end
end
The idea is to iterate the swapping loop until the ‘swap’ test is false ( i.e. no swap was made during a particular iteration of the vector ). Your loop sorts in descending order. You can add a counter to keep track of the number of swaps that were made.

5 Kommentare

Shawn Simon
Shawn Simon am 31 Okt. 2015
Oh! From what Walter Roberson answered, I realized that I was making only one pass through the array, when I should be making multiple passes. So, if I understand your code correctly, the while loop is making the for loop run multiple times so every number is accounted for and placed in the appropriate position?
Shawn Simon
Shawn Simon am 31 Okt. 2015
Also, is there any real purpose for the B = A; other than just having the original vector stored in a different variable?
Star Strider
Star Strider am 31 Okt. 2015
Correct. You need one loop (the for loop) to do the swaps, and another loop (the while loop) to keep track of them.
The purpose of B=A is to address your concerns: ‘One of the things I do not know how to do is place a value of one array into a new array...’ Since I didn’t understand what you meant by that, I created ‘B’ so that you have a copy of the original vector to compare to the sorted one, if you need it.
Shawn Simon
Shawn Simon am 31 Okt. 2015
Awesome!
Star Strider
Star Strider am 31 Okt. 2015
Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 31 Okt. 2015

1 Stimme

B(m) = A(n);
for some m and n.
However, that is not the reason your code is not working. You have implemented one pass of a "bubble sort". If your input were [3 2 1] then when n=1 it would change to [2 3 1] and then when n=2 it would change to [2 1 3] (and in the next pass it would crash because you would be trying to compare A(3) to the non-existent A(4), but that is trivially fixed.) Notice that the "2" and the "1" never get compared to each other. You will need to re-examine your strategy to fix that.

1 Kommentar

Shawn Simon
Shawn Simon am 31 Okt. 2015
Bearbeitet: Shawn Simon am 31 Okt. 2015
So, even though I used a while loop, I would need a second loop (like a for loop used by Star Strider) to make multiple passes through the array to ensure that each element of the array is compared to each other?

Melden Sie sich an, um zu kommentieren.

timo
timo am 31 Okt. 2015

0 Stimmen

Dont reinvent the wheel
>> x =py.list([5,4,1,2,3,7])
x =
Python list with no properties.
[5.0, 4.0, 1.0, 2.0, 3.0, 7.0]
>> x.sort
>> x
x =
Python list with no properties.
[1.0, 2.0, 3.0, 4.0, 5.0, 7.0]

3 Kommentare

Star Strider
Star Strider am 31 Okt. 2015
‘I am trying to create a script in which I sort an array in descending order without using any sort function.’
timo
timo am 1 Nov. 2015
but why ? Too much free time ?
Star Strider
Star Strider am 1 Nov. 2015
It could be just to see how a sort script works, and to learn programming processes and logic in the process. Some people have the sort of insatiable curiosity about such things that will carry them far. Shawn is likely one such person.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by