help with sort function
Mostra commenti meno recenti
I am writing my own sorting function, I've gotten the minimum value for the array and set that to the other array, and I couldn't figure out how to get rid of that minimum value in the original array and tell the program to find the next smallest minimum value. Here's my code so far
clear,clc;
x = [6 5 3 5 9 10 438 4 1 4 7 0 4 8 4 2];
len = length(x);
minval = x(1);
for n = 1:len
for i = 1:len
if x(i) < minval
minval = x(i);
end
end
sortval(n) = minval;
end
disp(sortval)
I was thinking maybe I could delete that value, but doing that would change the size of the array and it wouldn't work.
1 Commento
Hi Zhi
It's rewarding to be able to develop your own tools, yet sometimes, if time is of essence, on budget, perhaps you would like to consider having a look at a lost of already implemented sorting algorithms, here:
The one you want to implement, i think it's the bubble one.
Regards
John BG
Risposta accettata
Più risposte (1)
the cyclist
il 14 Mar 2017
Modificato: the cyclist
il 14 Mar 2017
I have not looked in detail at your algorithm. But one possibility that springs to mind is that you could swap NaN in place of the element you want to eliminate. That will retain the size of the array, and NaN will never be selected as less than any other element, because the logical operation
NaN < x
will return false for any value of x.
3 Commenti
zhi zhu
il 14 Mar 2017
the cyclist
il 14 Mar 2017
Can you post the new code?
zhi zhu
il 14 Mar 2017
Categorie
Scopri di più su Shifting and Sorting Matrices in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!